127 lines
3.0 KiB
C#
127 lines
3.0 KiB
C#
using System.Numerics;
|
|
using System.Runtime.Intrinsics;
|
|
|
|
namespace Lesson10_封装_运算符重载
|
|
{
|
|
//class Vector
|
|
//{
|
|
// public int x;
|
|
// public int y;
|
|
// public Vector(int x,int y)
|
|
// {
|
|
// this.x = x;
|
|
// this.y = y;
|
|
// }
|
|
// public static bool operator ==(Vector a, Vector b)
|
|
// {
|
|
// if (a.x == b.x && a.y == b.y)
|
|
// {
|
|
// return true;
|
|
// }
|
|
// else
|
|
// {
|
|
// return false;
|
|
// }
|
|
// }
|
|
// public static bool operator !=(Vector a, Vector b)
|
|
// {
|
|
// if (a.x != b.x && a.y != b.y)
|
|
// {
|
|
// return true;
|
|
// }
|
|
// else
|
|
// {
|
|
// return false;
|
|
// }
|
|
// }
|
|
//}
|
|
struct Position
|
|
{
|
|
int x;
|
|
int y;
|
|
|
|
public Position(int x,int y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
public static bool operator ==(Position a, Position b)
|
|
{
|
|
if (a.x == b.x && a.y == b.y)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
public static bool operator !=(Position a, Position b)
|
|
{
|
|
if (a.x != b.x && a.y != b.y)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
class Vector3
|
|
{
|
|
public int x,y,z;
|
|
public Vector3()
|
|
{
|
|
x = 0;
|
|
y = 0;
|
|
z = 0;
|
|
}
|
|
public Vector3(int x,int y,int z)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
this.z = z;
|
|
}
|
|
public static Vector3 operator +(Vector3 a,Vector3 b)
|
|
{
|
|
Vector3 vector = new Vector3();
|
|
vector.x = a.x + b.x;
|
|
vector.y = a.y + b.y;
|
|
vector.z = a.z + b.z;
|
|
return vector;
|
|
}
|
|
public static Vector3 operator -(Vector3 a, Vector3 b)//一步到位写法
|
|
{
|
|
return new Vector3(a.x-b.x,a.y-b.y,a.z-b.z);
|
|
}
|
|
public static Vector3 operator *(Vector3 a, int b)
|
|
{
|
|
Vector3 vector = new Vector3();
|
|
vector.x = a.x * b;
|
|
vector.y = a.y * b;
|
|
vector.z = a.z * b;
|
|
return vector;
|
|
}
|
|
}
|
|
internal class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Console.WriteLine("Hello, World!");
|
|
Position p1 = new Position(1, 2);
|
|
Position p2 = new Position(1, 2);
|
|
Console.WriteLine(p1 == p2);
|
|
Console.WriteLine(p1 != p2);
|
|
Vector3 v1 = new Vector3(2,2,2);
|
|
Vector3 v2 = new Vector3(1,1,1);
|
|
Vector3 v3 = v1 + v2;
|
|
Vector3 v4 = v1 - v2;
|
|
Vector3 v5 = v1 * 2;
|
|
Console.WriteLine(v3.x);
|
|
Console.WriteLine(v4.x);
|
|
Console.WriteLine(v5.x);
|
|
}
|
|
}
|
|
}
|