Java:在超类中初始化公共静态字段,该字段在每个子类实例中需要不同的值
晚上好,
我正在开发一组 Java 类,以便容器类 Box
包含包含类 Widget
的 List
。 Widget
需要能够指定与其他 Widget
的关系。我认为一个好方法是这样做:
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();
}
所以 Widget
的子类可以:
public class BlueWidget extends Widget {
public static final WidgetID WIDGETID = new WidgetID();
@Override
public WidgetID getWidgetID() {
return WIDGETID;
}
}
现在,BlueWidget
可以做 getBox().addWidgetRelationship( ConnectionTypes.SomeType、RedWidget.WIDGETID
和 Box
可以迭代其列表,将第二个参数与 iter.next().getWidgetID()
进行比较
。到目前为止,我要做的就是不必在所有子类中声明 public static final WidgetID WIDGETID
,而是在父 Widget
中实现它。问题是,如果我将该行代码移到 Widget
中(以及 getWidgetID()
的实现),那么 子类的每个实例都会被删除。 code>Widget
似乎为其 Subclassname.WIDGETID
获得相同的 static Final WidgetID
但是,使其成为非静态意味着我什至无法再调用 < code>Subclassname.WIDGETID
那么:如何在父 Widget
类中创建静态 WidgetID
,同时确保它对于每个 实例都是不同的。 Widget
和 Widget
的子类?或者我在这里使用了错误的工具来完成这项工作?
谢谢!
[编辑] 我不希望要求库的用户在所有子 Widget
构造函数中调用 super()
。
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().addWidgetRelationship(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
既然这个想法似乎是 WidgetID 对于每个特定的子类都是唯一的,为什么不直接通过它们的类来识别它们呢?
因此,您在执行
getBox().addWidgetRelationship(RelationshipTypes.SomeType, RedWidget.WIDGETID)
的地方,您可以执行getBox().addWidgetRelationship(RelationshipTypes.SomeType, RedWidget.class)
反而。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 dogetBox().addWidgetRelationship(RelationshipTypes.SomeType, RedWidget.class)
instead.如果每个子类都有不同的值,则该变量不是超类的成员。
换句话说,是的,每个子类都应该声明自己的WIDGETID;您无法将各个类的这些成员合并为超类中的单个成员。
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.
为什么不在 Widget 中声明一个抽象方法,例如:
这样,每个子类都必须实现该方法并返回自己的值。您仍然可以在子类中将 WidgetID 声明为静态,但上面的实例方法返回“唯一”值。
Why not just declare an abstract method within Widget such as:
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.