如何使用接口或抽象确保类具有静态属性? [英] How can I assure a class to have a static property by using interface or abstract?

查看:51
本文介绍了如何使用接口或抽象确保类具有静态属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类-假设myBase.我希望所有从myBase派生的类都具有一个名为

I have one abstract class -let's say myBase. And I want all the classes derived from myBase to have one static field called

public static List<string> MyPArameterNames 
{
get {return _myParameterNames;} 
}

因此,每个子类都可以知道其使用的参数名称;我想要静态的,因为我不想为此创建实例.

So, every child class can tell what parameter names it uses; I want static because I do not want to create an instance just for this.

我该如何实现?

推荐答案

您不能这样做.接口,抽象等不能应用于静态成员.如果要完成此操作,则必须手动记住在所有派生类上执行此操作.

You can't do that. Interfaces, abstract, etc. cannot apply to static members. If you want to accomplish this, you will have to manually remember to do it on all deriving classes.

此外,静态成员由派生类继承.子类如果希望指定替代行为,则必须隐藏静态父成员.

Also, static members are inherited by deriving classes. Child classes must hide the static parent member if they wish to specify alternate behavior.

这篇关于如何使用接口或抽象确保类具有静态属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆