Object 類別

定義

支援 .NET 類別階層中的所有類別,並提供衍生類別的低階服務。 這是所有 .NET 類別的最終基類;它是類型階層的根目錄。

public ref class System::Object
public class Object
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
public class Object
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Object
type obj = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
type obj = class
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type obj = class
Public Class Object
屬性

範例

以下範例定義了從類別 Object 衍生出的點型態,並覆蓋了該 Object 類別的許多虛擬方法。 此外,範例展示了如何呼叫該類別的 Object 許多靜態與實例方法。

using System;

// The Point class is derived from System.Object.
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.
        var 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 $"({x}, {y})";
    }

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

public sealed class App
{
    static void Main()
    {
        // Construct a Point object.
        var p1 = new Point(1,2);

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

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

        // The line below displays false because p1 and p2 refer to two different objects.
        Console.WriteLine(Object.ReferenceEquals(p1, p2));

        // 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));

        // The line below displays true because p1 and p3 refer to one object.
        Console.WriteLine(Object.ReferenceEquals(p1, p3));

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

// This code example produces the following output:
//
// False
// True
// True
// p1's value is: (1, 2)
//
open System

// The Point class is derived from System.Object.
type Point(x, y) =
    member _.X = x
    member _.Y = y
    override _.Equals obj =
        // If this and obj do not refer to the same type, then they are not equal.
        match obj with
        | :? Point as other ->
            // Return true if  x and y fields match.
            x = other.X &&  y = other.Y
        | _ -> 
            false

    // Return the XOR of the x and y fields.
    override _.GetHashCode() =
        x ^^^ y

    // Return the point's value as a string.
    override _.ToString() =
        $"({x}, {y})"

    // Return a copy of this point object by making a simple field copy.
    member this.Copy() =
        this.MemberwiseClone() :?> Point

// Construct a Point object.
let p1 = Point(1,2)

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

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

// The line below displays false because p1 and p2 refer to two different objects.
printfn $"{Object.ReferenceEquals(p1, p2)}"

// The line below displays true because p1 and p2 refer to two different objects that have the same value.
printfn $"{Object.Equals(p1, p2)}"

// The line below displays true because p1 and p3 refer to one object.
printfn $"{Object.ReferenceEquals(p1, p3)}"

// The line below displays: p1's value is: (1, 2)
printfn $"p1's value is: {p1.ToString()}"
// This code example produces the following output:
//
// False
// True
// True
// p1's value is: (1, 2)
//
' The Point class is derived from System.Object.
Class Point
    Public x, y As Integer
    
    Public Sub New(ByVal x As Integer, ByVal y As Integer) 
        Me.x = x
        Me.y = y
    End Sub
    
    Public Overrides Function Equals(ByVal obj As Object) As Boolean 
        ' If Me and obj do not refer to the same type, then they are not equal.
        Dim objType As Type = obj.GetType()
        Dim meType  As Type = Me.GetType()
        If Not objType.Equals(meType) Then
            Return False
        End If 
        ' Return true if  x and y fields match.
        Dim other As Point = CType(obj, Point)
        Return Me.x = other.x AndAlso Me.y = other.y
    End Function 

    ' Return the XOR of the x and y fields.
    Public Overrides Function GetHashCode() As Integer 
        Return (x << 1) XOR y
    End Function 

    ' Return the point's value as a string.
    Public Overrides Function ToString() As String 
        Return $"({x}, {y})"
    End Function

    ' Return a copy of this point object by making a simple field copy.
    Public Function Copy() As Point 
        Return CType(Me.MemberwiseClone(), Point)
    End Function
End Class  

NotInheritable Public Class App
    Shared Sub Main() 
        ' Construct a Point object.
        Dim p1 As New Point(1, 2)
        
        ' Make another Point object that is a copy of the first.
        Dim p2 As Point = p1.Copy()
        
        ' Make another variable that references the first Point object.
        Dim p3 As Point = p1
        
        ' The line below displays false because p1 and p2 refer to two different objects.
        Console.WriteLine([Object].ReferenceEquals(p1, p2))

        ' 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))

        ' The line below displays true because p1 and p3 refer to one object.
        Console.WriteLine([Object].ReferenceEquals(p1, p3))
        
        ' The line below displays: p1's value is: (1, 2)
        Console.WriteLine($"p1's value is: {p1.ToString()}")
    
    End Sub
End Class
' This example produces the following output:
'
' False
' True
' True
' p1's value is: (1, 2)
'

備註

類別 Object 是所有 .NET 類別的最終基類;它是類型階層的根目錄。

由於 .NET 中的所有類別都是衍生自 Object,因此類別中 Object 定義的每個方法都可在系統中的所有物件中使用。 衍生類別可以覆寫其中一些方法,包括:

  • Equals:支援對象之間的比較。
  • Finalize:在自動回收物件之前,執行清除作業。
  • GetHashCode:產生對應至 物件值的數位,以支援哈希表的使用。
  • ToString:製造人類可讀取的文字字串,描述 類別的實例。

語言通常不需要類別來宣告繼承, Object 因為繼承是隱含的。

效能考量

如果您要設計一個類別,比如一個必須處理任何類型物件的集合,您可以建立接受 Object 類別實例的類別成員。 不過,資料型別的裝箱和拆箱過程會帶來效能損耗。 如果您知道新類別會經常處理某些實值型別,您可以使用兩種策略之一,將 Boxing 的成本降到最低。

  • 建立可接受Object類型的通用方法,以及一組特定類型的方法多載,以處理您預期類別經常處理的每種實值類型。 如果存在一個接受呼叫參數類型的類型特定方法,則不會發生裝箱,並且會叫用該類型特定方法。 如果沒有與呼叫參數類型相匹配的方法參數,則會將參數封裝並調用一般方法。
  • 設計您的類型及其成員以使用 泛型。 當您建立 類別的實例並指定泛型類型自變數時,Common Language Runtime 會建立封閉式泛型類型。 泛型方法具有類型專屬特性,可以在不需要將呼叫參數裝箱的情況下被叫用。

雖然有時需要開發接受和傳回 Object 型別的一般用途類別,但您也可以提供類型特定的類別來處理常用型別來改善效能。 例如,提供用於設置及獲取布爾值的特定類別,可以消除裝箱和拆箱布爾值的開銷。

建構函式

名稱 Description
Object()

初始化 Object 類別的新執行個體。

方法

名稱 Description
Equals(Object, Object)

判斷指定的物件實例是否被視為相等。

Equals(Object)

判斷指定的物件是否等於目前的物件。

Finalize()

允許對象嘗試釋放資源,並在垃圾收集回收之前執行其他清除作業。

GetHashCode()

做為預設哈希函式。

GetType()

取得目前實例的 Type

MemberwiseClone()

建立目前 Object的淺層複本。

ReferenceEquals(Object, Object)

判斷指定的 Object 實例是否為同一實例。

ToString()

傳回表示目前 物件的字串。

適用於

執行緒安全性

此類型的公用靜態 (Shared) 成員是安全線程。 實例成員不保證執行緒安全。