zoukankan      html  css  js  c++  java
  • C# 终极基类Object介绍

    一、简介

    Object这个类型,相信everyone都不陌生,这个是CLR定义的最基础的类型,俗称"上帝类"。CLR(运行时)要求所有类型,不管是系统定义的类型还是自定义的类型,都必须从Object派生,所以以下代码从本质上是一样的,代码如下:

    /// <summary>
    /// 隐式派生自Object
    /// </summary>
    class Worker
    { 
            
    }
    
    /// <summary>
    /// 显式派生自Object
    /// </summary>
    class Worker : System.Object
    { 
            
    }

    因为CLR会要求所有的类型都派生自Object,所以自定义类就算没有显示继承Object类,CLR还是会让自定义类默认继承Object类,由于所有的类型都从System.Object类型派生,所以每个类型的每个对象(实例)都会有一组最基本的方法。

    二、Object公开的实例方法

    以下一派生自Object之后自带的公开的实例方法:

    上面4个方法其中Equals、ToString、GetHashCode这三个方法是虚方法,可重写GetType是外部方法.下面来一一介绍:

    1、Equals方法

    如果两个对象具有相同的值,就返回true,详情请参考C# 对象相等性判断和同一性判断

    2、GetHashCode方法

    返回对象的值的哈希值,详情请参考C# 对象哈希码

    3、ToString方法

    默认返回类型的完整名称(this.GetType().FullName)。例如,核心类型(如Boolean和Int32)类型重写该方法来返回他们的值的字符串表示,另外处于调试的目地而重写该方法.调用后获得一个字符串,显示对象各字段的值.代码如下:

            static void Main(string[] args)
            {
                var t = new Test
                {
                    Name = "张三",
                    Desc = "张三的描述",
                    Age = 23
                };
                Console.WriteLine(t.ToString());
                Console.ReadKey();
            }
            public class Test
          {
            public string Name { get; set; }
    
            public string Desc { get; set; }
    
            public int Age { get; set; }
    
            public override string ToString()
            {
                var type = this.GetType();
                PropertyInfo[] infos = type.GetProperties();
                StringBuilder sb = new StringBuilder();
                foreach (var property in infos)
                {
                    if (property.GetIndexParameters().Length == 0)
                    {
                        string propertyName = property.Name;
                        string propertyType = property.PropertyType.Name;
                        var propertValue = property.GetValue(this);
                        if (property.GetIndexParameters().Length == 0)
                            sb.AppendFormat("属性名:{0},属性类型:{1},属性值:{2}", propertyName, propertyType, propertValue);
                        else
                            sb.AppendFormat("当前属性为索引属性,属性名为:{0},属性值:{1}", propertyName, propertyType);
                        sb.AppendLine();
                    }
                    
                }
                return sb.ToString();
            }
        }    

    4、GetType方法

    返回从一个Type派生的一个类型的实例,指出调用GetType的那个对象是什么类型,返回的Type对象和反射类配合,获取与对象的类型有关的元数据信息.GetType是非虚方法,目的是防止类重写该方法,隐瞒起类型,破坏类型的安全性,代码如下:

        public class Program
        {
            static void Main(string[] args)
            {
                var p = new Person
                {
                    Name = "张三",
                    Age = 23
                };
                var t = p.GetType();
                var properties = t.GetProperties();
                foreach (var item in properties)
                {
                    var propertyName = item.Name;
                    var propertyVal = item.GetValue(p);
                    Console.WriteLine("属性名:{0},属性值:{1}", propertyName, propertyVal);
                }
                Console.ReadKey();
            }
        }
        public class Person
        {
            public string Name { get; set; }
    
            public int Age { get; set; }
        }

     三、Object受保护的方法

    1、MemberwiseClone方法

    这个非虚方法创建类型的新实例,并将新对象的实例字段设于this对象的实例字段完全一致,返回对新实例的引用,代码如下:

        public class Program
        {
            static void Main(string[] args)
            {
                var p = new Person
                {
                    Name = "张三",
                    Age = 11
                };
                var t=p.Clone();
                Console.WriteLine("this is Original object named p,the value of property named Name is {0},this value of property named Age is {1}", p.Name, p.Age);
                Console.WriteLine("this is clone object named t,the value of property named Name is {0},this value of property named Age is {1}", t.Name, t.Age);
                Console.ReadKey();
            }
        }
        public class Person
        {
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public Person Clone()
            {
                return (Person)MemberwiseClone();
            }
        }

    2、Finalize方法

    在垃圾回收器判断对象应该作为垃圾被回收之后,在对象的内存实际被回收之前,会调用这个虚方法.需要在回收内存前执行清理工作的类型应该重写该方法.

  • 相关阅读:
    从C#到TypeScript
    从C#到TypeScript
    从C#到TypeScript
    从C#到TypeScript
    UWP开源项目 LLQNotifier 页面间通信利器(移植EventBus)
    .net源码分析
    读读日报小布版 计划与反馈
    Pyinstaller使用
    Pyspider安装使用
    元器件资料查看网站
  • 原文地址:https://www.cnblogs.com/GreenLeaves/p/7479412.html
Copyright © 2011-2022 走看看