c#//新建类 Student.cs
class Student {
private int age;
private string name;
private long id;
public Student(int age, string name, long id) {
this.age = age;
this.name = name;
this.id = id;
}
// 带函数方法开始 == !=
public static bool operator == (Student s1, Student s2) {
if (s1.age == s2.age && s1.name == s2.name && s1.id == s2.id) {
return true;
}
return false;
}
public static bool operator != (Student s1, Student s2) {
bool result = s1 == s2;
return !result;
}
// 带函数方法结束
}
c#//使用 Program.cs
Student s1 = new Student(20, "张三", 12903);
Student s2 = new Student(20, "张三", 12903);
Student s3 = s1;
Console.WriteLine(s1 == s2);
Console.WriteLine(s1 == s3);
// 上面方法带函数方法 比较的是参数值 值都为True
// 上面方法不带函数方法 比较的是引用 值为 Flase True
本文作者:千纸鹤
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!