简体   繁体   English

接口中的Java静态方法/字段(再次!)

[英]Java static methods/fields in interface (again!)

my problem is: 我的问题是:

  • I have a bunch of different classes all extending a base class ( Identifiable ). 我有一堆不同的类,它们都扩展了基类( Identifiable )。
  • I need to assign to some of the sub-class a certain value ( securityLevel ) which should be changeable and assigned to all member of the class (ie: it should be static ). 我需要为某些子类分配一定的值( securityLevel ),该值应该可以更改并分配给该类的所有成员(即:它应该是static )。
  • I need to access the classes via the common ancestor. 我需要通过共同祖先访问这些类。
  • How do I do this? 我该怎么做呢?

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). 首先想到的是拥有一个专用interfaceISecurity ),它定义值或静态方法来访问它们,并让实际的类不implements它,并且如果这样做,则覆盖static字段(或检索它的方法)。 However this is not possible for two reasons: 但是,由于两个原因,这是不可能的:

  1. The current Java language does not allow static members in interface s. 当前的Java语言不允许interface s中的static成员。
  2. Even if it would allow it it would not be possible to @Override it. 即使允许,也无法@Override

How can I code around the problem? 我该如何解决该问题? The only way I found is: 我发现的唯一方法是:

  • add a non- static member ( public Class getValueProvider() ) to base class to retrieve the value returning null . 向基类添加一个非static成员( public Class getValueProvider() )以检索返回null的值。
  • in the interested classes @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()实现所需值的设置器和获取器。
  • use the retrieved 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM