我可以拥有一个基类,其中每个派生类都有自己的静态属性副本吗?
我有类似下面的情况:
class Base
{
public static int x;
public int myMethod()
{
x += 5;
return x;
}
}
class DerivedA : Base
{
}
class DerivedB : Base
{
}
我正在尝试设置它,以便每个派生类都有自己的 x 静态实例,如果我执行如下操作:
DerivedA.x = 5;
DerivedB.x = 10;
那么当我运行时:
DerivedA.myMethod(); //The result will be 10
DerivedB.myMethod(); //The reusult will be 15
我可以执行类似的操作吗?如何设置派生类来实现此目的?谢谢你们。
编辑:基本上,我有一堆派生类,每个派生类都有该类独有的属性。它对于每个实例都没有变化,因此我认为应该是一个静态变量。此外,该属性是通过方法设置的,该方法对于每个类都是相同的。 我试图避免在每个派生类中复制并粘贴该属性和方法的逻辑。我认为最好以某种方式将该逻辑移至每个类所派生的基类。但是,我需要每个派生类都有自己的该属性的副本。 我不一定要这样做,如果你们有任何更好的做法建议,我将很高兴听到一些更好的做法建议..谢谢!
I have something like the following situation below:
class Base
{
public static int x;
public int myMethod()
{
x += 5;
return x;
}
}
class DerivedA : Base
{
}
class DerivedB : Base
{
}
I am trying to set this up so that each derived class has its own static instance of x, if I do something like this:
DerivedA.x = 5;
DerivedB.x = 10;
then when I run:
DerivedA.myMethod(); //The result will be 10
DerivedB.myMethod(); //The reusult will be 15
Can i do something like this? How can I setup the derived classes to achieve this? Thanks guys.
EDIT: Basically, I have a bunch of derived classes that each have a property unique to that class. It does not vary for each instance, and thus I believe should be a static variable. Also, that property is set by a method, that is the same for each of these classes.
I am trying to avoid having to copy and paste the logic for that property and method in each of these derived classes. I thought it best to somehow move that logic to the base class from which each of these classes are derived from. But, I need each derived class to have its own copy of that property.
I do not necessarily have to do it this way, and I will be happy to hear some better practices suggestions if you guys have any.. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您将需要重新定义并隐藏所有派生类型中的字段和方法。
示例:
注意:不要这样做。修复你的设计。
编辑:
实际上,我有类似的构造。我用一个抽象(如果您需要默认值,请使用
virtual
)属性来解决它,然后从基类中使用该属性:您应该能够根据您的用例调整它。当然,如果只有派生类能够看到该属性,您当然可以将其设置为受保护的。
You will need to redefine and hide the field and method in all derived types.
Example:
Note: don't do it this way. Fix your design.
Edit:
Actually, I have a similar construct. I solve it with an abstract (if you need a default value, use
virtual
) property which then gets used from the base class:You should be able to adjust that for your use case. You can of course make the property
protected
if only deriving classes should be able to see it.以 Type 作为键的静态字典应该可以。我的意思是避免逻辑重复和每个派生类型的不同值但在实例之间共享。
Static dictionary with Type as keys should do. I mean avoid logic repetition and different values for each derived type but shared among instances.
我通常将子类特定的东西实现为抽象 get 属性
I usually implement subclass specific stuff as an abstract get property
嗯,是的,你可以,但它涉及到一些泛型的技巧。
如果您修复设计以便不需要该静态字段,或者至少不需要每个后代,那就更好了,但这里是这样的:
这依赖于这样一个事实:具有静态字段的泛型类型将获得这些静态字段的单独副本您调用它的每种类型的静态字段。
但是,如果您打算从 DerivedA 或 DerivedB 下降,就会变得很棘手,所以我不建议沿着这条路走。
Well, yes, you can, but it revolves around a bit of a trick with generics.
It is much better if you fix your design so that you don't need that static field, or at least not per descendant, but here goes:
This relies on the fact that a generic type with static fields will get a separate copy of those static fields for each type you invoke it with.
However, if you intend to descend from DerivedA or DerivedB, it gets tricky, so I wouldn't recommend going down this lane.