C# 基礎(十九)C# Object 類的詳細解釋及應用 舉例
阿新 • • 發佈:2019-01-14
一、簡介
部落格參考將主要分析Object 是用來幹嘛的。它是 .NET Framework 中所有類的最終基類;它是型別層次結構的根。不管是系統定義的型別還是自定義的型別,都必須從Object派生。
參考:
網站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