C# 基础(十九)C# Object 类的详细解释及应用 举例

一、简介

博客参考将主要分析Object 是用来干嘛的。它是 .NET Framework 中所有类的最终基类;它是类型层次结构的根。不管是系统定义的类型还是自定义的类型,都必须从Object派生。

参考:

网站1、微软官网https://docs.microsoft.com/zh-cn/dotnet/api/system.object?redirectedfrom=MSDN&view=netframework-4.7.2

网站2、https://www.cnblogs.com/android-blogs/p/6494410.html

网站3、https://blog.csdn.net/wnln25/article/details/6678357

二、网站1:微软官网的案例及分析

先把代码放出来,用到了两个类。

program.cs: 

#region object类的应用举例。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            // Construct a Point object.
            Point p1 = new Point(1, 2);

            // Make another Point object that is a copy of the first.
            Point p2 = p1.Copy();

            // Make another variable that references the first Point object.
            Point p3 = p1;

            // The line below displays false because p1 and p2 refer to two different objects.
            Console.WriteLine(Object.ReferenceEquals(p1, p2));//ReferenceEquals表示比较对象。而p1、p2是不同的object,所以输出结果为false

            // The line below displays true because p1 and p2 refer to two different objects that have the same value.
            Console.WriteLine(Object.Equals(p1, p2));//Equals表示比较数值。而p1、p2有相同的数值(1,2),所以输出结果为true。

            // The line below displays true because p1 and p3 refer to one object.
            Console.WriteLine(Object.ReferenceEquals(p1, p3));//p1、p3是相同的object,所以输出结果为true

            // The line below displays: p1's value is: (1, 2)
            Console.WriteLine("p1's value is: {0}", p1.ToString());
            Console.ReadLine();
        }
    }
}
#endregion

Point.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class Point
    {
        public int x, y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public override bool Equals(object obj)
        {
            // If this and obj do not refer to the same type, then they are not equal.
            if (obj.GetType() != this.GetType()) return false;

            // Return true if  x and y fields match.
            Point other = (Point)obj;
            return (this.x == other.x) && (this.y == other.y);
        }

        // Return the XOR of the x and y fields.
        public override int GetHashCode()
        {
            return x ^ y;
        }

        // Return the point's value as a string.
        public override String ToString()
        {
            return String.Format("({0}, {1})", x, y);
        }

        // Return a copy of this point object by making a simple field copy.
        public Point Copy()
        {
            return (Point)this.MemberwiseClone();
        }
    }
}

先看输出结果:

False
True
True
p1's value is: (1, 2)

然后我们看单步调试:

所以,我们要obj转为Point类型: 

Point other = (Point)obj;

此时,

return (this.x == other.x) && (this.y == other.y);

other就是Point类型节点,我们就行直接拿出来比较即可:

三、网站3的代码

这篇博客,只包含了Pragram.cs文件。它和官网关于object类的分析,几乎一致。

但是我们需要注意下面的转换,这是常用转换格式,特地列出,给我提醒:

 C c = obj as C;//往往采用这种格式将object类型转化你想要的类型。我的Socket学习博客,也采用到这种格式转换。

好了,举个特例延伸一下,你将obj转为String、Socket、Point等等:

String  str = obj as String;
Socket socket = obj as Socket;
Point point = obj as Point; //string、Socket、Point是系统自定的类

Pragram.cs:

#region object类的应用举例2。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace test
{
    struct A
    {
        public int count;
    }
    class B
    {
        public int number;
    }

    class C
    {
        public int integer = 0;
        public override bool Equals(object obj)
        {
            C c = obj as C;//往往采用这种格式将object类型转化你想要的类型。我的Socket学习博客,也采用到这种格式转换。
            if (c != null)
                return this.integer == c.integer;
            else
                return false;
        }
        public override int GetHashCode()
        {
            return 2 ^ integer;
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            A a1, a2;
            a1.count = 10;
            a2 = a1;

            //Console.Write(a1==a2);没有定义“==”操作符
            Console.Write(a1.Equals(a2));//True
            Console.WriteLine(object.ReferenceEquals(a1, a2));//False

            B b1 = new B();
            B b2 = new B();

            b1.number = 10;
            b2.number = 10;
            Console.Write(b1 == b2);//False
            Console.Write(b1.Equals(b2));//False
            Console.WriteLine(object.ReferenceEquals(b1, b2));//False

            b2 = b1;
            Console.Write(b1 == b2);//True
            Console.Write(b1.Equals(b2));//True
            Console.WriteLine(object.ReferenceEquals(b1, b2));//True

            C c1 = new C();
            C c2 = new C();

            c1.integer = 10;
            c2.integer = 10;
            Console.Write(c1 == c2);//False
            Console.Write(c1.Equals(c2));//True
            Console.WriteLine(object.ReferenceEquals(c1, c2));//False

            c2 = c1;
            Console.Write(c1 == c2);//True
            Console.Write(c1.Equals(c2));//True
            Console.WriteLine(object.ReferenceEquals(c1, c2));//True
 
        }
    }
}
#endregion

好了,object就是这么简单。

好了,object就是这么简单

好了,object就是这么简单

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/86302990