在每个派生类中具有基类静态成员的单独副本

发布于 2024-10-22 17:21:23 字数 675 浏览 2 评论 0原文

我有以下类结构:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2024-10-29 17:21:23

根本不要让它静态。这行不通吗?

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() and HandleEvents() to PresenterBase.

旧人 2024-10-29 17:21:23

要直接回答这个问题,您可以使基类通用,这将为您提供单独的静态字典,这是否是一个好的设计是另一个问题。

public abstract class PresenterBase<T> where T : PresenterBase<T>
{
    public static Dictionary<string, MethodInfo> methodsList = 
         new Dictionary<string,MethodInfo>();

}

public class DevicePresenter : PresenterBase<DevicePresenter>
{

}

public class HomePresenter : PresenterBase<HomePresenter>
{

} 

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.

public abstract class PresenterBase<T> where T : PresenterBase<T>
{
    public static Dictionary<string, MethodInfo> methodsList = 
         new Dictionary<string,MethodInfo>();

}

public class DevicePresenter : PresenterBase<DevicePresenter>
{

}

public class HomePresenter : PresenterBase<HomePresenter>
{

} 
澜川若宁 2024-10-29 17:21:23

我不会将其设为静态,因为您希望每个实例都有自己的列表,将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 value

That way you ensure that any derived class must provide this, but each instance has its own list.

I would also define abstract methods ShowScreen() and HandleEvents() to the PresenterBase as @Michael Kjorling suggests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文