在每个派生类中具有基类静态成员的单独副本
我有以下类结构:
public abstract class PresenterBase
{
public static Dictionary<string, MethodInfo> methodsList;
public void Bind()
public void Initialize();
}
public class DevicePresenter: PresenterBase
{
public void ShowScreen();
public void HandleEvents();
}
public class HomePresenter: PresenterBase
{
public void ShowScreen();
public void HandleEvents();
}
我想让 HomePresenter 和 DevicePresenter 拥有在 PresenterBase 中定义的 methodList 静态成员的单独副本。
不幸的是,他们与上述实现共享相同的副本。
他们是替代方法吗?我可以为 HomePresenter 和 DevicePresenter 提供方法列表的单独副本吗? 我不愿意在派生类中定义methodsList,因为将来如果有人添加另一个派生类,他必须记住将methodsList 添加到该类。
I have following class structure:
public abstract class PresenterBase
{
public static Dictionary<string, MethodInfo> methodsList;
public void Bind()
public void Initialize();
}
public class DevicePresenter: PresenterBase
{
public void ShowScreen();
public void HandleEvents();
}
public class HomePresenter: PresenterBase
{
public void ShowScreen();
public void HandleEvents();
}
I want to have HomePresenter and DevicePresenter to have separate copy of methodsList static member defined in PresenterBase.
Unfortunately they share the same copy with above implementation.
Is they alternative approach, that I can have separate copy of methodsList for HomePresenter and DevicePresenter?
I am not willing to define methodsList in derived classes because in future if someone adds another derived class he will have to keep in mind to add methodsList to that class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根本不要让它静态。这行不通吗?
static
表示与类型相关;非静态
表示与实例关联。我手边没有 Visual Studio 实例,但我相信您也可以在基类中标记字段
abstract
;那么编译器将要求您将其添加到任何派生类中。你绝对可以通过房产做到这一点。另一方面,鉴于上述代码,我可能会向
PresenterBase
添加抽象方法ShowScreen()
和HandleEvents()
。Don't make it static at all. Won't that work?
static
means associated with the type; non-static
means associated with the instance.I don't have a Visual Studio instance handy, but I believe you could also mark the field
abstract
in the base class; then the compiler will require you to add it to any deriving classes. You can definitely do that with a property.On another note, given the above code, I would probably add abstract methods
ShowScreen()
andHandleEvents()
toPresenterBase
.要直接回答这个问题,您可以使基类通用,这将为您提供单独的静态字典,这是否是一个好的设计是另一个问题。
To answer the question directly, you could make the base class generic, this will give you seperate static dictionaries, if this is a good design or not is another question.
我不会将其设为静态,因为您希望每个实例都有自己的列表,将methodsList定义为PresenterBase的属性,并在PresenterBase中定义一个构造函数,该构造函数采用
Dictionary
并设置propety to this value这样您就可以确保任何派生类都必须提供此值,但每个实例都有自己的列表。
我还将按照 @Michael Kjorling 的建议为
PresenterBase
定义抽象方法ShowScreen()
和HandleEvents()
I would not make it static, as you want each instance to have its own list, define methodsList as a property of PresenterBase and define a constructor in PresenterBase which takes the
Dictionary<string, MethodInfo>
and sets the propety to this valueThat way you ensure that any derived class must provide this, but each instance has its own list.
I would also define abstract methods
ShowScreen()
andHandleEvents()
to thePresenterBase
as @Michael Kjorling suggests