开发者

Java: Initializing a public static field in superclass that needs a different value in every subclass instance

开发者 https://www.devze.com 2022-12-27 02:40 出处:网络
Good evening, I am developing a set of Java classes so that a container class Box contains a List of a contained class Widget. A Widget needs to be able to specify relationships with other Widgets. I

Good evening,

I am developing a set of Java classes so that a container class Box contains a List of a contained class Widget. A Widget needs to be able to specify relationships with other Widgets. I figured a good way to do this would be to do something like this:

public abstract class Widget {
    public static class WidgetID {
        // implementation stolen from Google's GWT
        private static int nextHashCode;
        private final int index;

        public WidgetID() {
            index = ++nextHashCode;
        }

        public final int hashCode() {
            return index;
        }
    }

    public abstract WidgetID getWidgetID();

}

so sublcasses of Widget could:

public class BlueWidget extends Widget {
    public static final WidgetID WIDGETID = new WidgetID();

    @Override
    public WidgetID getWidgetID() {
        return WIDGETID;
    }
}

Now, BlueWidget can do getBox().addWidgetRela开发者_运维技巧tionship(RelationshipTypes.SomeType, RedWidget.WIDGETID, and Box can iterate through it's list comparing the second parameter to iter.next().getWidgetID().

Now, all this works great so far. What I'm trying to do is keep from having to declare the public static final WidgetID WIDGETID in all the subclasses and implement it instead in the parent Widget class. The problem is, if I move that line of code into Widget (along with the implementation of getWidgetID()), then every instance of a subclass of Widget appears to get the same static final WidgetID for their Subclassname.WIDGETID. However, making it non-static means I can no longer even call Subclassname.WIDGETID.

So: how do I create a static WidgetID in the parent Widget class while ensuring it is different for every instance of Widget and subclasses of Widget? Or am I using the wrong tool for the job here?

Thanks!

[Edit] I would prefer not to require users of the library to call super() in all their sub-Widget constructors.


Since it seems like the idea is that the WidgetID be unique for each specific subclass, why don't you just identify them by their class instead?

So where you are doing getBox().addWidgetRelationship(RelationshipTypes.SomeType, RedWidget.WIDGETID) you could do getBox().addWidgetRelationship(RelationshipTypes.SomeType, RedWidget.class) instead.


If every subclass has a different value, the variable isn't a member of the superclass.

In other words, yes, each subclass should declare its own WIDGETID; you cannot consolidate those members of various classes as a single member in a superclass.


Why not just declare an abstract method within Widget such as:

protected abstract WidgetID getWidgetID();

This way, each subclass must implement the method and return its own value. You can still declare WidgetID to be static within the subclasses, but the above instance method to return the 'unique' value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号