Java:抽象类中的静态字段
我只是从一个例子开始,它解释得最好:
public abstract class A{
static String str;
}
public class B extends A{
public B(){
str = "123";
}
}
public class C extends A{
public C(){
str = "abc";
}
}
public class Main{
public static void main(String[] args){
A b = new B();
A c = new C();
System.out.println("b.str = " + b.str);
System.out.println("c.str = " + c.str);
}
}
这将打印出:
b.str = abc
c.str = abc
但我想要一个解决方案,其中实例化超类的每个子类都有自己的 /strong> 类变量,同时我希望能够通过抽象超类中定义的标识符或方法调用来引用该类变量。
所以我希望输出为:
b.str = 123
c.str = abc
这可行吗?
I just start out with an example, that explains it best:
public abstract class A{
static String str;
}
public class B extends A{
public B(){
str = "123";
}
}
public class C extends A{
public C(){
str = "abc";
}
}
public class Main{
public static void main(String[] args){
A b = new B();
A c = new C();
System.out.println("b.str = " + b.str);
System.out.println("c.str = " + c.str);
}
}
This will print out:
b.str = abc
c.str = abc
But I would like a solution where each subclass that instantiate the super class, has their own class variable, at the same time I want to be able to reference that class variable through the identifier, or a method call, defined in the abstract super class.
So I would like the output to be:
b.str = 123
c.str = abc
Is that doable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您希望类 B 和 C 有单独的静态变量,则需要在这些类中声明变量。基本上,静态成员和多态性不能并存。
请注意,从可读性角度来看,通过引用访问静态成员确实是一个坏主意 - 它使得它看起来像是依赖于引用的值,而实际上它并不依赖于引用的值。真的。因此,当您将
str
向下移动到 B 和 C 时,您当前的代码甚至无法编译。 相反,您需要如果您确实需要多态地访问该值(即通过 A 的实例) )那么一种选择是制作一个多态 getter:(
对于 C 来说也是如此)。
这样,您就可以获得所需的行为,即每个实例都没有单独的变量,但您仍然可以多态地使用它。对于实例成员来说,返回这样的静态值有点奇怪,但是您正在使用类型多态性的值,基本上......
If you want classes B and C to have separate static variables, you'll need to declare the variables in those classes. Basically, static members and polymorphism don't go together.
Note that accessing static members through references is a really bad idea in terms of readability - it makes it look like it depends on the value of the reference, when it doesn't really. So your current code won't even compile when you've moved
str
down to B and C. Instead, you'll needIf you really need to access the value polymorphically (i.e. through an instance of A) then one option is to make a polymorphic getter:
(and the same for C).
That way you get the behaviour you want in terms of not having a separate variable per instance, but you can still use it polymorphically. It's a little odd for an instance member to return a static value like this, but you're using the value for polymorphism of type, basically...
然后你就可以拥有
setter 和 getter 是我添加的,你可以简单地使变量成为非静态的。
更新 如果您想要每个子类都有静态,那么您可以:
然后:
但这通常是一个坏主意。
Then you'll be able to have
The setter and getter are my addition, you can go by simply making the variable non-static.
Update If you want to have the static-per-subclass, then you can have:
and then:
But this is generally a bad idea.
将静态变量放入每个子类中,并向抽象超类添加一个(非静态)抽象方法:
然后通过返回该特殊子类的静态字段来在每个子类中实现 getStr() 方法。
Put the static varibale in each subclass and add a (not static) abstract method to the abstract superclass:
Then implement the getStr() method in each subclass by returning the static field of this special subclass.
系统中仅存在一个静态变量实例。
静态变量将在类加载之前加载到系统中。
两次打印 abc 的原因是因为您最后将 str 的值设置为 abc 。
only one instance of static variable is present in the system.
static variable will load into the system in the start before class is loaded.
reason both time abc is printed is because you set the value of str as abc in the last.
这将打印您想要的输出:
This will print the output you want:
由于无论如何您都在子类中对值或 str 进行了硬编码,因此您可以执行以下操作:
这将在您的情况下实现这一点。
当然,那么你应该通过方法来调用它,如下所示:
Since you hardcode the value or
str
in subclasses anyway, you can do something like this:This would do the trick in your case.
Of course, then you should call it by method, like this:
我这样做是为了避免必须在每个子类中实现相同的方法。这是基于Bozho的回答。也许它可以帮助某人。
It is what I did to avoid to have to implement the same method in every subclass. It is based on the answer of Bozho. Maybe it may help someone.
我认为解决这个问题的一种方法是使用类
B
和C
的单例来模拟静态方法和字段。它们都可以扩展抽象类A
,但会有自己的str
值。I think one way to approach this is to use a singleton for class
B
andC
to mimic static methods and fields. The can both extend abstract classA
, but will have their own values ofstr
..