覆盖基本属性 [英] Override base property
本文介绍了覆盖基本属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class A
{
public string prop
{
get{ ...some logic...; }
}
public string method()
{
string p = do something calculation using prop.
return p;
}
}
public class B
{
// i would like to override property prop,
// so when i call B.method from client, the return value will be calculated
//based on overrided property
}
我尝试过:
What I have tried:
if the property can be override, or only the method can be.
Is there any idea to implement this if keep the property.
推荐答案
属性可以被覆盖:
Properties can be overridden:
public class A
{
public virtual string prop
{
get { return "A:prop"; }
}
public string method()
{
string p = prop + " Added";
return p;
}
}
public class B : A
{
// i would like to override property prop,
// so when i call B.method from client, the return value will be calculated
//based on overrided property
public override string prop
{
get { return "B:prop"; }
}
}
private void MyButton_Click(object sender, EventArgs e)
{
A a = new A();
A b = new B();
Console.WriteLine("{0},{1}", a.prop, a.method());
Console.WriteLine("{0},{1}", b.prop, b.method());
将生成:
Will generate:
A:prop,A:prop Added
B:prop,B:prop Added
这篇关于覆盖基本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文