在Java中继承静态变量
我想要进行以下设置:
abstract class Parent {
public static String ACONSTANT; // I'd use abstract here if it was allowed
// Other stuff follows
}
class Child extends Parent {
public static String ACONSTANT = "some value";
// etc
}
这在java中可能吗?如何?如果可以避免的话,我宁愿不使用实例变量/方法。
谢谢!
编辑:
常量是数据库表的名称。每个子对象都是一个迷你 ORM。
I want to have the following setup:
abstract class Parent {
public static String ACONSTANT; // I'd use abstract here if it was allowed
// Other stuff follows
}
class Child extends Parent {
public static String ACONSTANT = "some value";
// etc
}
Is this possible in java? How? I'd rather not use instance variables/methods if I can avoid it.
Thanks!
EDIT:
The constant is the name of a database table. Each child object is a mini ORM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能完全按照你想要的方式去做。也许可以接受的妥协是:
you can't do it exactly as you want. Perhaps an acceptable compromise would be:
在这种情况下你必须记住在java中你不能覆盖静态方法。发生的事情是它隐藏了这些东西。
根据您放置的代码,如果您执行以下操作,
只要您使用 Parent 作为引用类型,答案将为 null,ACONSTANT 将为 null。
让你做这样的事情。
输出将为空。
In this case you have to remember is in java you can't overried static methods. What happened is it's hide the stuff.
according to the code you have put if you do the following things answer will be null
as long as you use Parent as reference type ACONSTANT will be null.
let's you do something like this.
Output will be null.