重写ToString方法? [英] Overriding ToString Method?
问题描述
请查看以下代码:
public class Panda
{
public string 名称;
public 覆盖 string ToString (){返回名称; }
}
...
Panda p = new Panda {Name = Petey};
Console.WriteLine(p); // Petey
因为我知道覆盖方法仅在类型超类的引用指向类型子类的对象时才起作用。我也知道ToString方法是System.Object类的成员。
我想让你为我描述一下这个代码(拳击,覆盖场景)。
谢谢
你的压倒一切都很好。你关心的是OOP非常难,你应该很清楚,否则你将无法正确处理任何类型的.NET工作。
首先,你需要看到差异在编译时类型和运行时类型之间。
public class 哺乳动物{
public 覆盖 string ToString(){ return / * ... * / ; } // 不必覆盖,例如
}
public class Panda:Mammal {
public 覆盖 string ToString(){ return / * ... * / ; } // 其他东西
}
// ...
Panda panda = 新熊猫(); // panda的运行时类型与设计时类型相同
哺乳动物someMammal = new Panda(); // someMammal的编译时类型为Mammal,
// 这就是编译器看到类型的方式;
// 它不会直接访问特定于Panda的名称
// < span class =code-comment>但对象的运行时类型是Panda
string someString = someMammal.ToString() ; // 将调用ToString()?
上述评论中问题的答案:对ToString()
的调用将被分派到Panda.ToString()
,尽管事实变量类型是Mammal
。 如果方法不是虚拟的,那么变量编译时的方法将被称为,Mammal
之一。这是OOP的重点:虚拟方法动态调度到实际运行时类型;对于非虚拟,调用根据编译时类型的方法。虚拟方法是动态调度的(它们是 后期 ),非虚拟 - 静态调度。
参见:
http ://en.wikipedia.org/wiki/Dynamic_dispatch [ ^ ],
http://en.wikipedia.org/wiki/Virtual_method_table [ ^ ],
http://en.wikipedia.org/wiki/Virtual_function [ ^ ]。
请看我过去的其他答案:
http://www.codeproject.com/Answers/597424/wherepluscanplusIplusfindplusinformationplusaboutp#answer1
< DD> -SA
Please take look at the following code:
public class Panda
{
public string Name;
public override string ToString() { return Name; }
}
...
Panda p = new Panda { Name = "Petey" };
Console.WriteLine (p); // Petey
as i know override methods only work when reference of type superclass points to object of type subclass.Also i know that ToString method is member of System.Object class.
I want you to describe this code for me(boxing,override scenarios).
thank you
Your overriding is just fine. Your concern is about the very hard of OOP, which you should understand well, otherwise you won't be able to proceed correctly with any kind of .NET works.
First, you need to see the difference between compile-time types and runtime types.
public class Mammal { public override string ToString() { return /* ... */; } // does not have to be overridden, just for example } public class Panda: Mammal { public override string ToString() { return /* ... */; } // something else } //... Panda panda = new Panda(); // panda's runtime type is the same as design-time type Mammal someMammal = new Panda(); // someMammal has the compile-time type Mammal, // this is how the type is seen by a compiler; // it won't access the names specific to Panda directly // but the object's runtime type is Panda string someString = someMammal.ToString(); // which ToString() will be called?
The answer to the question in the above comment: the call toToString()
will be dispatched toPanda.ToString()
, despite the fact the variable type isMammal
. If the method wasn't virtual, the method of the variable compile-time would be called, the one ofMammal
. This is the whole point of OOP: virtual methods are dispatch dynamically to the ones of the actual run-time type; for non-virtual, the method according to the compile-time type is called. Virtual methods are dispatches dynamically (they are late-bound), non-virtual — statically.
[EDIT]
See also:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^],
http://en.wikipedia.org/wiki/Virtual_function[^].
[EDIT]
Please see my other past answer:
http://www.codeproject.com/Answers/597424/wherepluscanplusIplusfindplusinformationplusaboutp#answer1
—SA
这篇关于重写ToString方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!