home commit with new plugins
This commit is contained in:
+71
-75
@@ -1,86 +1,82 @@
|
||||
# 运算符重载
|
||||
概念:
|
||||
==让自定义类和结构体能够使用运算符==
|
||||
让自定义类结构体可以进行运算
|
||||
通常需要注意运算符需要几个参数,例如取反只有一个参数,加有两个参数
|
||||
可重载的运算符:
|
||||
- 算数运算符
|
||||
- 逻辑运算符==非==
|
||||
- 位运算符
|
||||
- 条件运算符
|
||||
不可重载的运算符:
|
||||
- 逻辑与(&&)逻辑或(||)
|
||||
- 索引符\[]
|
||||
- 墙砖运算符()
|
||||
- 点.
|
||||
- 三目运算符: ?
|
||||
- 赋值符=
|
||||
|
||||
- 运算符重载可以让==自定义类型==像内置类型一样使用某些运算符
|
||||
- 本质上是给运算符提供一套你自己定义的行为
|
||||
|
||||
通常用法:
|
||||
|
||||
- 坐标、向量、复数、金额等“天然适合运算”的类型
|
||||
|
||||
语法:
|
||||
使用关键词operator
|
||||
作为类的一个成员存在
|
||||
public static 返回值类型 operator 运算符(参数列表)
|
||||
|
||||
案例:
|
||||
```Csharp
|
||||
class Point
|
||||
```csharp
|
||||
public static 返回值类型 operator 运算符(参数列表)
|
||||
{
|
||||
public int x;
|
||||
public int y;
|
||||
|
||||
public static Point operator +(Point p1, Point p2)
|
||||
{
|
||||
Point newPoint = new Point();
|
||||
newPoint.x = p1.x + p2.x;
|
||||
newPoint.y = p1.y + p2.y;
|
||||
|
||||
return newPoint;
|
||||
}
|
||||
|
||||
public static Point operator +(Point p1, int value)
|
||||
{
|
||||
Point newPoint = new Point();
|
||||
newPoint.x = p1.x + value;
|
||||
newPoint.y = p1.y + value;
|
||||
return newPoint;
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Point p1 = new Point();
|
||||
p1.x = 1;
|
||||
p1.y = 1;
|
||||
|
||||
Point p2 = new Point();
|
||||
p2.x = 2;
|
||||
p2.y = 2;
|
||||
|
||||
Point p3 = new Point();
|
||||
p3 = p1 + p2;
|
||||
Console.WriteLine(p3.x);
|
||||
|
||||
p3 = null;
|
||||
p3 = p1 + 10;
|
||||
Console.WriteLine(p3.x);
|
||||
}
|
||||
|
||||
// 自定义逻辑
|
||||
}
|
||||
```
|
||||
第一个运算符重载了`+`号,可以让这个类的两个实例化对象相加
|
||||
第二个运算符也重载了`+`号,可以让这个类加上一个value
|
||||
==如果需要value在前也需要进行一次重载!!!==
|
||||
|
||||
案例:
|
||||
```csharp
|
||||
class Point
|
||||
{
|
||||
public int X;
|
||||
public int Y;
|
||||
|
||||
public static Point operator +(Point p1, Point p2)
|
||||
{
|
||||
return new Point
|
||||
{
|
||||
X = p1.X + p2.X,
|
||||
Y = p1.Y + p2.Y
|
||||
};
|
||||
}
|
||||
|
||||
public static Point operator +(Point p, int value)
|
||||
{
|
||||
return new Point
|
||||
{
|
||||
X = p.X + value,
|
||||
Y = p.Y + value
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Point p1 = new Point { X = 1, Y = 1 };
|
||||
Point p2 = new Point { X = 2, Y = 2 };
|
||||
|
||||
Point p3 = p1 + p2;
|
||||
Point p4 = p1 + 10;
|
||||
Console.WriteLine($"{p3.X}, {p3.Y}");
|
||||
Console.WriteLine($"{p4.X}, {p4.Y}");
|
||||
```
|
||||
|
||||
补充:
|
||||
- 如果你希望 `10 + p1` 也成立,需要再额外写一份对应重载
|
||||
- 运算符重载最重要的是==语义自然==,不要让 `+` 去做奇怪的事
|
||||
|
||||
常见可重载运算符:
|
||||
- 一元:`+` `-` `!` `~` `++` `--` `true` `false`
|
||||
- 二元:`+` `-` `*` `/` `%` `&` `|` `^` `<<` `>>`
|
||||
- 比较:`==` `!=` `<` `>` `<=` `>=`
|
||||
|
||||
不能直接重载的常见运算符:
|
||||
- `&&` `||`
|
||||
- `[]`
|
||||
- `()`
|
||||
- `.`
|
||||
- `=`
|
||||
- `?:`
|
||||
- `??`
|
||||
|
||||
注意:
|
||||
1. 一定是公共的静态方法
|
||||
2. 返回值写在operator前
|
||||
3. 逻辑处理自定义
|
||||
4. 一个符号可以多个重载
|
||||
5. 不能使用ref和out
|
||||
6. ==条件运算符需要成对实现==
|
||||
1. ==传统写法里,运算符重载通常写成 `public static`==
|
||||
2. 参数里至少要有一个参数是当前类型
|
||||
3. 不能给运算符参数加 `ref` 或 `out`
|
||||
4. 某些运算符需要成对实现:
|
||||
- `==` 和 `!=`
|
||||
- `<` 和 `>`
|
||||
- `<=` 和 `>=`
|
||||
- `true` 和 `false`
|
||||
5. 如果重载了 `==` / `!=`,通常也应该一起重写 `Equals` 和 `GetHashCode`
|
||||
6. `&&` / `||` 不能直接重载,但可以结合 `true` / `false` 和 `&` / `|` 实现相关行为
|
||||
|
||||
#核心
|
||||
|
||||
Reference in New Issue
Block a user