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

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.lryc.cn/news/2397872.html

相关文章:

  • 多元素纳米颗粒:开启能源催化新纪元
  • 分布式锁优化:使用Lua脚本保证释放锁的原子性问题
  • 电脑wifi显示已禁用怎么点都无法启用
  • 【FPGA开发】Ubuntu16.04环境下配置Vivado2018.3—附软件包
  • vue-seamless-scroll 结束从头开始,加延时后滚动
  • 不同的数据库操作方式:MongoDB(NoSQL)和 MySQL/SQL
  • 0-EATSA-GNN:基于图节点分类师生机制的边缘感知和两阶段注意力增强图神经网络(code)
  • 大数据学习(124)-spark数据倾斜
  • 配置前端控制器
  • lua注意事项
  • Git的三种合并方式
  • 从零到一:我的技术博客导航(持续更新)
  • SpringBoot整合Flowable【08】- 前后端如何交互
  • DM达梦数据库开启SQL日志记录功能
  • 00 QEMU源码分析中文注释与架构讲解(v8.2.4版本)
  • 【五模型时间序列预测对比】Transformer-LSTM、Transformer、CNN-LSTM、LSTM、CNN
  • 深入了解MCP基础与架构
  • 实验设计与分析(第6版,Montgomery)第5章析因设计引导5.7节思考题5.13 R语言解题
  • 怎么选择合适的高防IP
  • 【java面试】MySQL篇
  • 贪心算法应用:欧拉路径(Fleury算法)详解
  • 【算法设计与分析】实验——二维0-1背包问题(算法分析题:算法思路),独立任务最优调度问题(算法实现题:实验过程,描述,小结)
  • P12592题解
  • ffmpeg命令(二):分解与复用命令
  • 【Git】View Submitted Updates——diff、show、log
  • deepseek原理和项目实战笔记2 -- deepseek核心架构
  • 在 MATLAB 2015a 中如何调用 Python
  • 房屋租赁系统 Java+Vue.js+SpringBoot,包括房屋类型、房屋信息、预约看房、合同信息、房屋报修、房屋评价、房主管理模块
  • 华为OD机试真题——生成哈夫曼树(2025B卷:100分)Java/python/JavaScript/C/C++/GO六种最佳实现
  • react与vue的渲染原理