为什么在 C# 中不涉及继承就可以重写 ToString() 方法?
Why ToString() method can be overridden without inheritance being involved in C#?
请查找以下示例:
using System;
class MyClass
{
static int count = 0;
int id;
public MyClass()
{
id = count;
count++;
}
public override string ToString()
{
return "MyClass object #" + id;
}
}
class Test
{
static void Main()
{
MyClass ob1 = new MyClass();
MyClass ob2 = new MyClass();
MyClass ob3 = new MyClass();
Console.WriteLine(ob1);
Console.WriteLine(ob2);
Console.WriteLine(ob3);
}
}
程序的输出如下所示:
MyClass 对象 #0
MyClass 对象 #1
MyClass 对象 #2
正如您在此处看到的,方法 ToString() 在名为 'MyClass' 的 class 中被覆盖,但 'MyClass' 不是从任何 class 派生的,不存在继承根本。如果不涉及继承,如何使用 'override' ?
如果我们将 ToString() 更改为任何其他方法名称,请说 'HelloString()',如以下代码片段所示:
public class Program
{
public static void Main(string[] args)
{
MyClass ob1 = new MyClass();
MyClass ob2 = new MyClass();
MyClass ob3 = new MyClass();
Console.WriteLine(ob1);
Console.WriteLine(ob2);
Console.WriteLine(ob3);
}
}
class MyClass
{
static int count = 0;
int id;
public MyClass()
{
id = count;
count++;
}
public override string HelloString()
{
return "MyClass object #" + id;
}
}
编译器抛出错误:'no suitable method found to override'
为什么 'ToString()' 方法会出现这种特殊行为?
它派生自 Object,这是定义该方法的地方。
每种类型都派生自 oop 中的对象,这就是定义 ToString() 方法及其虚拟方法的地方,因此您可以覆盖它
Object
有一个 ToString()
方法。每个 class 都继承自 Object
。
摘自 https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx.
Object class is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.
Languages typically do not require a class to declare inheritance from
Object because the inheritance is implicit.
Because all classes in the .NET Framework are derived from Object, every method defined in the Object class is available in all objects in the system. Derived classes can and do override some of these methods, including:
- Equals - Supports comparisons between objects.
- Finalize - Performs cleanup operations before an object is
automatically reclaimed.
- GetHashCode - Generates a number corresponding to the value of the
object to support the use of a hash table.
- ToString - Manufactures a human-readable text string that describes
an instance of the class.
ToString
方法被继承!
所有 classes 都隐式继承自一件事。并且结构也继承自它。通过装箱,您可以将结构分配给该类型的变量。
你能猜出是什么吗?
Object
!
此 class 包含诸如 ToString
、GetHashCode
、Equals
之类的方法,您可以覆盖这些方法来定义您自己的对象行为。你可以在这里看到更多:
https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx
真的,您的 MyClass 派生自对象 class。如果你使用
class MyClass : Object
结果是一样的
请查找以下示例:
using System;
class MyClass
{
static int count = 0;
int id;
public MyClass()
{
id = count;
count++;
}
public override string ToString()
{
return "MyClass object #" + id;
}
}
class Test
{
static void Main()
{
MyClass ob1 = new MyClass();
MyClass ob2 = new MyClass();
MyClass ob3 = new MyClass();
Console.WriteLine(ob1);
Console.WriteLine(ob2);
Console.WriteLine(ob3);
}
}
程序的输出如下所示:
MyClass 对象 #0
MyClass 对象 #1
MyClass 对象 #2
正如您在此处看到的,方法 ToString() 在名为 'MyClass' 的 class 中被覆盖,但 'MyClass' 不是从任何 class 派生的,不存在继承根本。如果不涉及继承,如何使用 'override' ?
如果我们将 ToString() 更改为任何其他方法名称,请说 'HelloString()',如以下代码片段所示:
public class Program
{
public static void Main(string[] args)
{
MyClass ob1 = new MyClass();
MyClass ob2 = new MyClass();
MyClass ob3 = new MyClass();
Console.WriteLine(ob1);
Console.WriteLine(ob2);
Console.WriteLine(ob3);
}
}
class MyClass
{
static int count = 0;
int id;
public MyClass()
{
id = count;
count++;
}
public override string HelloString()
{
return "MyClass object #" + id;
}
}
编译器抛出错误:'no suitable method found to override'
为什么 'ToString()' 方法会出现这种特殊行为?
它派生自 Object,这是定义该方法的地方。
每种类型都派生自 oop 中的对象,这就是定义 ToString() 方法及其虚拟方法的地方,因此您可以覆盖它
Object
有一个 ToString()
方法。每个 class 都继承自 Object
。
摘自 https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx.
Object class is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy.
Languages typically do not require a class to declare inheritance from Object because the inheritance is implicit.
Because all classes in the .NET Framework are derived from Object, every method defined in the Object class is available in all objects in the system. Derived classes can and do override some of these methods, including:
- Equals - Supports comparisons between objects.
- Finalize - Performs cleanup operations before an object is automatically reclaimed.
- GetHashCode - Generates a number corresponding to the value of the object to support the use of a hash table.
- ToString - Manufactures a human-readable text string that describes an instance of the class.
ToString
方法被继承!
所有 classes 都隐式继承自一件事。并且结构也继承自它。通过装箱,您可以将结构分配给该类型的变量。
你能猜出是什么吗?
Object
!
此 class 包含诸如 ToString
、GetHashCode
、Equals
之类的方法,您可以覆盖这些方法来定义您自己的对象行为。你可以在这里看到更多:
https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx
真的,您的 MyClass 派生自对象 class。如果你使用
class MyClass : Object
结果是一样的