当前位置: 首页 > ds >正文

20250530-C#知识:万物之父Object

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)); //FalseConsole.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)); //FalseConsole.WriteLine(Equals(point1, point2));  //TrueConsole.WriteLine(ReferenceEquals(daily1, daily2)); //FalseConsole.WriteLine(Equals(daily1, daily2));  //TrueType type = student1.GetType();Console.WriteLine(type.ToString()); //LearnObject.StudentStudent 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());  //18643596Console.WriteLine(student7.ToString()); //自定义输出,学生的名字:张飞,年龄:18Console.WriteLine(student8.ToString()); //LearnObject.Student2}}
}

6、参考资料:

  1. 《唐老狮C#》

本篇结束,感想您的阅阅阅阅阅阅阅阅阅读~

在这里插入图片描述

http://www.xdnf.cn/news/10123.html

相关文章:

  • 云原生应用架构设计原则与落地实践:从理念到方法论
  • 通信算法之280:无人机侦测模块知识框架思维导图
  • JS 事件循环详解
  • 告别重复 - Ansible 配置管理入门与核心价值
  • 在 Linux 上安装 Minikube:轻松搭建本地 Kubernetes 单节点集群
  • 项目管理工具Maven
  • java/mysql/ES下的日期类型分析
  • 【FlashRAG】本地部署与demo运行(二)
  • PHP7内核剖析 学习笔记 第九章 PHP基础语法的实现
  • [特殊字符] xbatis 一款好用 ORM 框架 1.8.8-M2 发布,节省 1/3 代码和时间的框架!!!
  • Drawio编辑器二次开发
  • 【pytorch学习】土堆pytorch学习笔记2
  • 【Linux】权限相关指令
  • Axure疑难杂症:中继器新增数据时如何上传并存储图片(玩转中继器)
  • 【仿生机器人系统设计】涉及到的伦理与安全问题
  • 数据基座觉醒!大数据+AI如何重构企业智能决策金字塔(上)
  • 代码随想录打卡|Day53 图论(Floyd 算法精讲 、A * 算法精讲 (A star算法)、最短路算法总结篇、图论总结 )
  • 历年武汉大学计算机保研上机真题
  • 服务器液冷:突破散热瓶颈,驱动算力革命的“冷静”引擎
  • 美国服务器文件系统的基本功能和命令
  • ansible-playbook 进阶 接上一章内容
  • dart实现单client连接的grpc server (以ip地址作判断)
  • Yum配置第三方源与本地源详解
  • 日常--OBS+mediamtx实现本地RTMP推流环境搭建(详细图文)
  • RPG17.蓝图函数库与轻重攻击连击
  • C++ 5.29 dddd
  • 【深度剖析】义齿定制行业数字化转型模式创新研究(上篇2:痛点和难点分析)
  • AI智能监控系统:赋能厂区安全管理的数智化革新
  • [Dify] 如何应对明道云API数据过长带来的Token超限问题
  • OAuth协议中的Token、Ticket