问题描述
我有一个要求,我必须有许多类都是从单个基类派生的.基类包含子类列表,这些子类也从相同的基类派生.
I have a requirement where I have a number of classes all derived from a single base class. The base class contains lists of child classes also derived from the same base class.
所有类都必须能够获取特定的值,这些值可以从类本身获取-或者-它是父类,具体取决于派生类是什么.
All classes need to be able to obtain specific values which may be obtained from the class itself -OR- it's parent depending on what the derived class is.
我看过使用方法而不是属性,但是我还想使这些值可用于.NET报告组件,该组件可直接访问报告引擎中公开的公共属性,因此不包括方法的使用.
I looked at using Methods rather than properties however I also want to make the values available to a .NET reporting component which directly accesses exposed public properties in the reporting engine so this excludes the use of methods.
我的问题是,在BaseClass中没有公开可用的setter的情况下,在DerivedClass中实现setter的最佳实践"方法是什么?
My question is what would be the 'best practices' method of implementing a setter in DerivedClass without having a publicly available setter in BaseClass
public class BaseClass
{
private BaseClass _Parent;
public virtual decimal Result
{
get { return ((_Parent != null) ? _Parent.Result : -1); }
}
}
public class DerivedClass : BaseClass
{
private decimal _Result;
public override decimal Result
{
get { return _Result; }
// I can't use a setter here because there is no set method in the base class so this throws a build error
//set { _Result = value; }
}
}
由于无法在DerivedClass中更改访问修饰符,因此无法在BaseClass中添加受保护的setter(如下所示).
I can't add a protected setter (as follows) in BaseClass as I cannot change access modifiers in DerivedClass.
public class BaseClass
{
private BaseClass _Parent;
public virtual decimal Result {
get { return ((_Parent != null) ? _Parent.Result : -1); }
protected set { }
}
}
public class DerivedClass : BaseClass
{
private decimal _Result;
public override decimal Result
{
get { return _Result; }
// This setter throws a build error because of a change of access modifiers.
//set { _Result = value; }
}
}
我不想在带有Setter的BaseClass中添加成员变量,因为我不希望能够从BaseClass或其他从base派生的类中设置属性.
I don't want to add a member variable in BaseClass with a setter as I do not want the ability to set the property from the BaseClass or other classes that derive from base.
public class BaseClass
{
private BaseClass _Parent;
protected Decimal _Result; // This could result in a lot of unnecessary members in BaseClass.
public virtual decimal Result {
get { return _Result; }
// Empty setter ahead :) This does nothing.
// I could also throw an exception here but then issues would not be found until runtime
// and could cause confusion in the future with new developers etc.
set { }
}
}
public class DerivedClass : BaseClass
{
public override decimal Result
{
get { return base.Result; }
set { base._Result = value; }
}
}
其他建议?
推荐答案
您可以使用'new'关键字,但这将替换属性,而覆盖则无法实现所需的属性.
You can use the 'new' keyword but this will replace property, what you want is not possible with override.
public class BaseClass
{
private BaseClass _Parent;
public virtual decimal Result
{
get { return ((_Parent != null) ? _Parent.Result : -1); }
}
}
public class DerivedClass : BaseClass
{
private decimal _Result;
public new decimal Result
{
get { return _Result; }
set { _Result = value; }
}
}
这篇关于C#如何在派生类中添加属性设置器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!