C#知识:万物之父Object
Object类(即object)是所有类的基类,这里面的方法还是需要好好了解一下。
1、Object类
- 是顶级父类,其他类默认都是Object类的子类(自定义类也会默认继承Object类)
- 可以用Object变量装载值类型和其他自定义类型对象
- Object类是引用类型
- 存在装箱拆箱
object x = 100; //装箱
int y = (int)x; //拆箱
2、Object类中的静态方法
2.1 静态方法Equals
- 判断两个参数是否相等
- 调用第一个参数的虚拟Equals方法进行判断,无论值类型和引用类型
public static bool Equals(object? objA, object? objB);
2.2 静态方法ReferenceEquals
- 比较两个引用类型对象是否相等
- 传递值类型则返回false
public static bool ReferenceEquals(object? objA, object? objB);
3、Object类的成员方法
3.1 普通方法GetType
- 返回对象类型的Type对象,可用于反射
public extern Type GetType();
3.2 普通方法MemberwiseClone
- 获取对象的浅拷贝对象
- 对象的引用类型成员变量也是直接拷贝,指向堆中相同位置
protected unsafe object MemberwiseClone();
4、Object类的虚拟方法
4.1 虚拟方法Equals
- 只有一个参数,将调用方法的对象与参数对象进行比较
- 引用类型比较引用(默认)
- 值类型比较值是否相等,因为System.ValueType中重写了该方法
- 测试了枚举和结构体,也符合值类型的比较规则
public virtual bool Equals(object? obj)
4.2 虚拟方法GetHashCode
- 获取对象哈希值
public virtual int GetHashCode()
4.3 虚拟方法ToString
- 将对象进行打印输出时默认调用此方法
public virtual string? ToString()
5、完整测试用例:
namespace LearnObject
{
enum Daily
{
Eat,
Sleep
}
struct Point
{
public int x;
public int y;
public Point():this(0,0) { }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Student
{
public string name;
public int age;
public Teacher teacher;
public Student():this("张飞",18, new Teacher()) { }
public Student(string name, int age)
{
this.name = name;
this.age = age;
this.teacher = new Teacher();
}
public Student(string name, int age, Teacher teacher) : this(name, age)
{
this.teacher = teacher;
}
public override bool Equals(Object? a)
{
if (a != null && a is Student)
{
Student other = a as Student;
return this.name.Equals(other.name) && this.age == other.age;
}
return false;
}
public override int GetHashCode()
{
Console.WriteLine($"重写之前的哈希值: {base.GetHashCode()}");
return 10086; //自定义哈希逻辑
}
public override string ToString()
{
return $"自定义输出,学生的名字:{this.name},年龄:{this.age} \n" ;
}
public Student ShallowClone() => this.MemberwiseClone() as Student;
}
class Student2
{
public string name;
public int age;
public Teacher teacher;
public Student2() : this("张飞", 18, new Teacher()) { }
public Student2(string name, int age)
{
this.name = name;
this.age = age;
this.teacher = new Teacher();
}
public Student2(string name, int age, Teacher teacher) : this(name, age)
{
this.teacher = teacher;
}
public Student2 ShallowClone() => this.MemberwiseClone() as Student2;
}
class Teacher
{
public string name;
public Teacher() { this.name = "诸葛亮"; }
public Teacher(string name)
{
this.name = name;
}
}
internal class Program
{
static void Main(string[] args)
{
object x = 100; //装箱
int y = (int)x; //拆箱
Student student1 = new Student();
Student student2 = new Student();
Student2 student3 = new Student2();
Student2 student4 = new Student2();
Console.WriteLine(Equals(student1, student2)); //True //因为Student中Equals方法被重写了
Console.WriteLine(Equals(student3, student4)); //False //因为Student2默认比较的是引用
Console.WriteLine(ReferenceEquals(student1, student2)); //False
Console.WriteLine(ReferenceEquals(5, 5));
Point point1 = new Point(1, 1); //测试结构体
Point point2 = new Point(1, 1);
Daily daily1 = Daily.Sleep; //测试枚举
Daily daily2 = Daily.Sleep;
Console.WriteLine(ReferenceEquals(point1, point2)); //False
Console.WriteLine(Equals(point1, point2)); //True
Console.WriteLine(ReferenceEquals(daily1, daily2)); //False
Console.WriteLine(Equals(daily1, daily2)); //True
Type type = student1.GetType();
Console.WriteLine(type.ToString()); //LearnObject.Student
Student student5 = new Student("关羽",20, new Teacher("诸葛亮"));
Student student6 = student5.ShallowClone();
Console.WriteLine("修改前");
Console.WriteLine($"学生5:{student5.name}的老师是{student5.teacher.name}"); //学生5:关羽的老师是诸葛亮
Console.WriteLine($"学生6:{student6.name}的老师是{student6.teacher.name}"); //学生6:关羽的老师是诸葛亮
student6.name = "刘备";
student5.teacher.name = "庞士元";
Console.WriteLine("学生6修改后");
Console.WriteLine($"学生5:{student5.name}的老师是{student5.teacher.name}"); //学生5:关羽的老师是庞士元
Console.WriteLine($"学生6:{student6.name}的老师是{student6.teacher.name}"); //学生6:刘备的老师是庞士元
Student student7 = new Student();
Student2 student8 = new Student2();
Console.WriteLine(student7.GetHashCode()); //10086
Console.WriteLine(student8.GetHashCode()); //18643596
Console.WriteLine(student7.ToString()); //自定义输出,学生的名字:张飞,年龄:18
Console.WriteLine(student8.ToString()); //LearnObject.Student2
}
}
}
6、参考资料:
- 《唐老狮C#》
本篇结束,感想您的阅阅阅阅阅阅阅阅阅读~