实时示例覆盖? [英] real time example overriding ?
问题描述
任何人都可以分享覆盖的时间示例,而不使用任何虚拟课程,
例如。
公共类A
{
public int添加()
{
返回10;
}
}
公共舱B:A
{
public int添加()
{
返回11;
}
}
Main()
{
一个obj =新B();
}
在哪种情况下我们需要指向子类的对象需要绑定到基类?
can anybody share time example of overriding, without using any dummy classes,
eg.
public class A
{
public int Add()
{
return 10;
}
}
public class B:A
{
public int Add()
{
return 11;
}
}
Main()
{
A obj = new B();
}
in which scenario we need to point object of child class need to bind to base class??
推荐答案
不使用任何虚拟课程是吗? OK ...
查看标准的ToString方法:
它由对象实现
class(从中派生所有其他类)并返回一个字符串,该字符串是对象的全名。
大多数(但不是全部)派生类重写此项以提供与实际实例内容更相关的字符串:int
覆盖它以返回整数值的字符串表示形式,bool
覆盖它以返回true或false,依此类推。如果你创建自己的类,你也可以覆盖它:
"without using any dummy classes" eh? OK...
Look at the standard ToString method:
It is implemented by theobject
class (from which all the other classes are derived) and it returns a string which is the full name of the object.
Most (but not all) derived classes override this to provide a string that is more relevant to the actual instance content:int
overrides it to return a string representation of the integer value,bool
overrides it to return either "true" or "false", and so on. If you create your own class, you can also override it:
public class MyClass
{
public string Name { get; set; }
public DateTime InsertDate { get; set; }
public override string ToString()
{
return Name + " " + InsertDate.ToShortDateString();
}
}
当您在实例上调用ToString时,系统会计算出最高级别的覆盖并调用而不是默认的 object
implementation。
When you call ToString on an instance, the system works out the highest level override and calls that instead of the default object
implementation.
这篇关于实时示例覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!