C#学习之向量运算符重载

//未重载运算符+=,但编译通过;C#不允许重载运算符‘=’,但如果重载‘+’运算符,编译器会自动使用‘+’运算符的重载来执行‘+=’运算符的操作;
//‘-=’、‘*=’、‘&=’和‘/=’等所有赋值运算都遵循此规则;

1、若重载了‘==’运算符,也必须重载‘!=’;否则会产生编译器错误

运算符重载示例(注意:左右运算符):

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using static System.Console;
 7 
 8 namespace LearnForCSharp
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             int i = 10;
15             WriteLine($"{i}");
16             int j = (int)(i + 0.8);
17             WriteLine($"{j}");
18             string iName = nameof(i);
19             Write(iName + ";iName:" + nameof(iName) + "\n");
20             WriteLine();
21 
22             Vector vector1, vector2, vector3;
23 
24             vector1 = new Vector(3, -1, 3);
25             vector2 = new Vector(4, 4, 4);
26             vector3 = vector1 + vector2 * 3;
27             Vector v4 = new Vector(vector3);
28 
29             v4 += vector2;
30             //未重载运算符+=,但编译通过;C#不允许重载运算符‘=’,但如果重载‘+’运算符,编译器会自动使用‘+’运算符的重载来执行‘+=’运算符的操作;
31             //‘-=’、‘*=’、‘&=’和‘/=’等所有赋值运算都遵循此规则;
32 
33             double multip = vector1 * vector3;
34             WriteLine($"{vector3.ToString()}");
35             WriteLine($"{multip}");
36             WriteLine($"{v4.ToString()}");
37             ReadKey();
38         }
39     }
40 
41     struct Vector
42     {
43         public double x, y, z;
44 
45         public Vector(double x, double y, double z)
46         {
47             this.x = x;
48             this.y = y;
49             this.z = z;
50         }
51 
52         public Vector(Vector rhs)
53         {
54             x = rhs.x;
55             y = rhs.y;
56             z = rhs.z;
57         }
58 
59         public override string ToString()
60         {
61             return "(" + x.ToString() + "," + y.ToString() + "," + z.ToString() + ")";
62         }
63 
64         public static Vector operator +(Vector lhs, Vector rhs)
65         {
66             Vector result = new Vector(lhs);
67 
68             result.x += rhs.x;
69             result.y += rhs.y;
70             result.z += rhs.z;
71 
72             return result;
73         }
74 
75         public static Vector operator *(double a, Vector rhs)
76         {
77             Vector result = new Vector(rhs);
78             result.x *= a;
79             result.y *= a;
80             result.z *= a;
81 
82             return result;
83         }
84 
85         public static Vector operator *( Vector rhs, double a)
86         {
87             return a * rhs;
88         }
89 
90         public static double operator *(Vector lhs, Vector rhs)
91         {
92             double result = 0;
93 
94             result = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
95 
96             return result;
97         }
98     }
99 }

 比较运算符重载"=="和“!=”

 1         //若比较俩个对象引用就是比较存储对象的内存地址,一般很少这样比较;比较对象的值,并返回布尔结果
 2         public static bool operator ==(Vector lhs, Vector rhs)
 3         {
 4             if (lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z)
 5                 return true;
 6             else
 7                 return false;
 8         }
 9 
10         public static bool operator !=(Vector lhs, Vector rhs)
11         {
12             return !(lhs == rhs);
13         }

 

转载于:https://www.cnblogs.com/RedCoffeeGod/p/5678730.html

你可能感兴趣的:(c#)