Object 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
支持 .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 Point 类型,并重写类的许多虚拟方法 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 类成员。 但是,类型装箱和取消装箱进程会产生性能成本。 如果指导新类会经常处理某些值类型,则可以使用两种策略之一来最大限度地降低装箱成本。
- 创建接受 Object 类型的常规方法,以及一组特定于类型的方法重载,以便接受预计类会经常处理的每种值类型。 如果存在特定于类型的方法,并且该方法接受调用参数类型,则不会进行装箱,并且会调用此特定于类型的方法。 如果没有与调用参数类型匹配的方法参数,则会对参数进行装箱,并调用常规方法。
- 将类型及其成员设计为使用泛型。 在创建类的实例并指定泛型类型参数时,公共语言运行时将创建关闭的泛型类型。 泛型方法是类型特定的,无需将调用参数装箱即可调用。
尽管有时有必要开发接受和返回 Object 类型的常规用途类,但还可以提供特定于类型的类来处理常用类型,从而提高性能。 例如,提供专用于设置和获取布尔值的类,以便消除布尔值装箱和取消装箱成本。
构造函数
| 名称 | 说明 |
|---|---|
| Object() |
初始化 Object 类的新实例。 |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object, Object) |
确定指定的对象实例是否被视为相等。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 |
| Finalize() |
允许对象在垃圾回收回收资源之前尝试释放资源并执行其他清理操作。 |
| GetHashCode() |
用作默认哈希函数。 |
| GetType() |
获取当前实例的 Type。 |
| MemberwiseClone() |
创建当前 Object的浅表副本。 |
| ReferenceEquals(Object, Object) |
确定指定的 Object 实例是否为同一实例。 |
| ToString() |
返回一个表示当前对象的字符串。 |
适用于
线程安全性
此类型的公共静态(Shared 在 Visual Basic 中)成员是线程安全的。 不保证实例成员是线程安全的。