[英]Don’t call subclass methods from a superclass constructor
Consider the following code 请考虑以下代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package example0;
/**
*
* @author yccheok
*/
public class Main {
static class A {
private final String var;
public A() {
var = getVar();
// Null Pointer Exception.
System.out.println("var string length is " + var.length());
}
public String getVar() {
return "String from A";
}
}
static class B extends A {
private final String bString;
// Before B ever constructed, A constructor will be called.
// A is invoking a overriden getVar, which is trying to return
// an initialized bString.
public B() {
bString = "String from B";
}
@Override
public String getVar() {
return bString;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
B b = new B();
}
}
Currently, in my mind, there are two ways to avoid such problem. 目前,在我看来,有两种方法可以避免这种问题。
Either making class A final class. 要么是A级的最后一堂课。
static final class A {
private final String var;
public A() {
var = getVar();
// Null Pointer Exception.
System.out.println("var string length is " + var.length());
}
public String getVar() {
return "String from A";
}
}
Or 要么
Making getVar method final 使getVar方法最终
static class A {
private final String var;
public A() {
var = getVar();
// Null Pointer Exception.
System.out.println("var string length is " + var.length());
}
public final String getVar() {
return "String from A";
}
}
The author trying to suggest ways to prevent the above problem. 作者试图提出防止上述问题的方法。 However, the solution seems cumbersome as there are some rules to be followed.
但是,解决方案似乎很麻烦,因为有一些规则需要遵循。
http://benpryor.com/blog/2008/01/02/dont-call-subclass-methods-from-a-superclass-constructor/ http://benpryor.com/blog/2008/01/02/dont-call-subclass-methods-from-a-superclass-constructor/
Beside making final and the author suggested way, is there more ways to prevent the above problem (Don't call subclass methods from a superclass constructor) from happen? 除了最终和作者建议的方式之外,还有更多方法可以防止上述问题(不要从超类构造函数中调用子类方法)吗?
Making getVar method final
使getVar方法最终
This is definitely what you need to do. 这绝对是你需要做的。
If you're relaying on the functionality of a method to initialize an object, you shouldn't let subclasses broke that method. 如果您正在转发初始化对象的方法的功能,则不应让子类破坏该方法。
Answering your question, other way to prevent it is to make getVar
private in A
. 回答你的问题, 其他阻止它的方法是在
A
中使getVar
私有。
See this simplified version of your code: 查看代码的简化版本:
// A.java
class A {
private final String var;
public A(){
var = getVar();
var.length();
}
private String getVar(){
return "This is the value";
}
}
class B extends A {
private final String other;
public B(){
other = "Other string";
}
public String getVar(){
return other;
}
}
class Main{
public static void main( String [] args ) {
new B();
}
}
BTW, why did you put those as static nested classes, just to create confusion? 顺便说一句,你为什么把那些作为静态嵌套类,只是为了造成混乱?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.