从具体子类中抽象类中提供值的“正确”方式是什么?从子类中提供抽象类成员变量
即,我应该这样做:
abstract class A {
private string m_Value;
protected A(string value) {
m_Value = value;
}
public string Value {
get { return m_Value; }
}
}
class B : A {
B() : this("string value") {}
}
或本:
abstract class A {
protected A() { }
public abstract string Value { get; }
}
class B : A {
B() {}
public override string Value {
get { return "string value"; }
}
}
或其他什么东西?
如果仅在抽象类中使用Value
属性,应该做不同的事情吗?
您可以通过声明虚拟属性来实现第一种方法。 – 2009-11-04 16:49:30