基本概念
public static Class operator+ (Class a, Class b)
使用案例
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.length=2;
car1.speed = 120;
Car car2 = new Car();
car2.length = 3;
car2.speed = 90;
// 将两个car对象相加
Car c3 = car1 + car2;
Console.WriteLine(c3); // output: length:5,speed:210
}
}
public class Car
{
///
/// 长度
///
public int length;
///
/// 速度
///
public int speed;
// 重载 对象的 +
public static Car operator +(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length + c2.length;
c3.speed = c1.speed + c2.speed;
return c3;
}
// 重写tostring方法,方便打印对象信息
public override string ToString()
{
return string.Format("length:{0},speed:{1}", this.length, this.speed);
}
}
}
**如果不使用重载也想要达成以上效果,作法如下:
**
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.length=2;
car1.speed = 120;
Car car2 = new Car();
car2.length = 3;
car2.speed = 90;
// 将两个car对象相加
Car car3 = new Car();
car3.length = car1.length + car2.length;
car3.speed = car1.speed + car2.speed;
Console.WriteLine(car3); // output: length:5,speed:210
}
}
public class Car
{
///
/// 长度
///
public int length;
///
/// 速度
///
public int speed;
// 重载 对象的 +
public static Car operator +(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length + c2.length;
c3.speed = c1.speed + c2.speed;
return c3;
}
// 重写tostring方法,方便打印对象信息
public override string ToString()
{
return string.Format("length:{0},speed:{1}", this.length, this.speed);
}
}
}
可以看到效果,如果不重载运算符,则需要在外面调用时 自己去计算,以上只有一处调用,是否重载无所谓,但是如果 调用的地方多了,每个调用的地方都得写此逻辑就太麻烦了,所以用好运算符重载还是很好用的
重载多个运算符实现
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Car car1 = new Car();
car1.length = 2;
car1.speed = 120;
// 利用重载的 true/false 运算符
if (car1)
{
Console.WriteLine(car1); // output: length:2,speed:120
}
Car car2 = new Car();
car2.length = 3;
car2.speed = 90;
// 将两个car对象相加
Car c3 = car1 + car2;
Console.WriteLine(c3); // output: length:5,speed:210
// 相减
c3 = car1 - car2;
Console.WriteLine(c3); // output: length:-1,speed:30
// 比较
if (car1 != car2)
{
Console.WriteLine("我们不相等");
}
}
}
public class Car
{
///
/// 长度
///
public int length;
///
/// 速度
///
public int speed;
// 重载 对象的 +
public static Car operator +(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length + c2.length;
c3.speed = c1.speed + c2.speed;
return c3;
}
// 重载 对象的 -
public static Car operator -(Car c1, Car c2)
{
Car c3 = new Car();
c3.length = c1.length - c2.length;
c3.speed = c1.speed - c2.speed;
return c3;
}
// 重载 对象的 ==, 注 == 和 != 是一对的,重写了一个,两个就都必须重写
public static bool operator ==(Car c1, Car c2)
{
return c1.length == c2.length && c1.speed == c2.speed;
}
// 重载 对象的 !=
public static bool operator !=(Car c1, Car c2)
{
return c1.length != c2.length || c1.speed != c2.speed;
}
// 重载 对象的 true, true和false也是一对
public static bool operator true(Car c1)
{
return c1.length != 0 || c1.speed != 0;
}
// 重载 对象的 false
public static bool operator false(Car c1)
{
return c1.length == 0 && c1.speed == 0;
}
// 重写tostring方法,方便打印对象信息
public override string ToString()
{
return string.Format("length:{0},speed:{1}", this.length, this.speed);
}
}
}
可重载和不可重载的运算****符
可被重载的 | 不可被重载的 |
---|---|
+, -, !, ~, ++, --,*, /, % | &&, |
==, !=, <, >, <=, >= | =, ., ?:, ->, new, is, sizeof, typeof |
全部0条评论
快来发表一下你的评论吧 !