[英]Java static methods/fields in interface (again!)
my problem is: 我的问题是:
Identifiable
). 我有一堆不同的类,它们都扩展了基类( Identifiable
)。 securityLevel
) which should be changeable and assigned to all member of the class (ie: it should be static
). 我需要为某些子类分配一定的值( securityLevel
),该值应该可以更改并分配给该类的所有成员(即:它应该是static
)。 The first thing which came to mind is to have a dedicated interface
( ISecurity
) defining either the values or a static method to access them and let the actual classes either not to implements
it and, if they do, to override the static
field (or method to retrieve it). 首先想到的是拥有一个专用interface
( ISecurity
),它定义值或静态方法来访问它们,并让实际的类不implements
它,并且如果这样做,则覆盖static
字段(或检索它的方法)。 However this is not possible for two reasons: 但是,由于两个原因,这是不可能的:
static
members in interface
s. 当前的Java语言不允许interface
s中的static
成员。 @Override
it. 即使允许,也无法@Override
。 How can I code around the problem? 我该如何解决该问题? The only way I found is: 我发现的唯一方法是:
static
member ( public Class getValueProvider()
) to base class to retrieve the value returning null
. 向基类添加一个非static
成员( public Class getValueProvider()
)以检索返回null
的值。 @Override
the non- static
method returning the value of a private static Class getValueProvider()
implementing setters and getters for the wanted value. 在感兴趣的类@Override
,非static
方法返回private static Class getValueProvider()
的值, private static Class getValueProvider()
实现所需值的设置器和获取器。 class
instance to obtain the requested value (or skip everything if the return is null). 使用检索到的class
实例获取请求的值(如果返回为null,则跳过所有操作)。 This is very ugly and there's no way to enforce the correct implementation in sub-classes. 这非常丑陋,无法在子类中强制执行正确的实现。
You could define Identifiable
to be an abstract class
. 您可以将Identifiable
定义为abstract class
。 Additionally, you can define another abstract class
that extends Identifiable
and adheres to your restrictions, ie holds the static variable
and whatever methods may be necessary. 此外,您可以定义另一个abstract class
,该abstract class
可扩展Identifiable
并遵守您的限制,即持有static variable
和任何可能需要的方法。
I would try to avoid any static members. 我会尽量避免使用任何静态成员。 Static members in java are always clamsy (you cannot override just hide them, etc.) Java中的静态成员总是笨拙的(您不能覆盖它们而仅将其隐藏,等等)
I'm not sure if I understand your problem corret but I suggest you construct the objects with a context interface or something. 我不确定我是否理解您的问题,但是我建议您使用上下文接口或其他方式构造对象。 The objects then cann access these context interface if they area allowed to return a value or have to return a special value. 如果对象允许区域返回值或必须返回特殊值,则它们无法访问这些上下文接口。
The one creating all these objects can pass the same object and so control the behaviour. 创建所有这些对象的对象可以传递相同的对象,从而控制行为。 This object could then be held static (like a singelton) 然后可以将该对象保持静态(如singelton)
You could try a service/factory type of implementation. 您可以尝试服务/工厂类型的实现。 Or have some sort of class object that stores security ( SecuritySettings
) and send in the current Identifiable
object to get security level 或具有某种存储安全性的类对象( SecuritySettings
)并发送当前的Identifiable
对象以获取安全级别
public class Identifiable { }
public class SampleUser extends Identifiable { }
public class ExampleUser extends Identifiable { }
public class UserService
{
public int SampleUserSecurity = 0;
//Or an array/dictionary structure
public int GetSecurityLevel(Identifiable user)
{
if(user instanceof SampleUser)
{
return SampleUserSecurity;
}
}
public SampleUser CreateSampleUser()
{
return new SampleUser();
}
public ExampleUser CreateExampleUser()
{
return new ExampleUser();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.