Java - 应该通过getters和setters方法在构造函数中访问私有实例变量吗? [英] Java - Should private instance variables be accessed in constructors through getters and setters method?

查看:298
本文介绍了Java - 应该通过getters和setters方法在构造函数中访问私有实例变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



但是当我在IDE的帮助下生成构造函数时,它直接初始化实例变量而不是通过他们的setter方法初始化它们。



Q1。因此,我应该更改IDE生成的构造函数代码,以通过它们的setter方法初始化这些实例变量。



Q2。如果是的话,为什么IDE不以这种方式生成构造函数代码?



===================== ========== EDITED ===================================




  • 我使用Eclipse和Netbeans IDE。


  • 一个一般问题。但是根据@Lords的要求,答案取决于我们的构造函数是public还是protected,还是私有还是私有?



解决方案

您应该从不从构造函数调用非最终方法。类构造函数用于初始化对象,并且对象在构造函数返回之前不处于一致状态。如果你的构造函数调用了一个非最终的方法,后来被一个子类覆盖,你可能会得到奇怪的,意想不到的结果,因为对象在调用重写的方法时没有完全初始化。



考虑这个假设的例子:

  class A {
private int x;

public A(){
setX(2);
}

public void setX(int x){
this.x = x;
}

public int getX(){
return x;
}
}

B类扩展A {
private int number = 10;

@Override
public void setX(int x){
//将x设置为数字的值:10
super.setX(number);
}
}

public class Test {
public static void main(String [] args){
B b = new B();
// b.getX()应该是10,对不对?
System.out.println(B.getX()=+ b.getX());
}
}

此程序的输出为:

  B.getX()= 0 

原因是 B 成员在此时未初始化 setX ,因此使用其默认值 0



本文有更全面的解释,有效的Java


I know that private instance variables are accessed through their public getters and setters method.

But when I generate constructors with the help of IDE, it initializes instance variables directly instead of initializing them through their setter methods.

Q1. So should I change the IDE generated code for constructors to initialize those instance variables through their setter methods.

Q2. If yes, then why IDE don't generate constructors code in that way?

============================= EDITED =======================================

  • I use Eclipse and Netbeans IDE

  • It's a general question. But as asked by @Lords would the answer depends on whether our constructor is public or protected or package private or private?

解决方案

You should never call a non-final method from a constructor. A class constructor is used to initialize an object, and the object is not in a consistent state until the constructor returns. If your constructor calls a non-final method which is later overridden by a subclass, you can get strange, unexpected results because the object is not fully initialized when the overridden method is called.

Consider this contrived example:

class A {
    private int x;

    public A() {
        setX(2);
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }
}

class B extends A {
    private int number = 10;

    @Override        
    public void setX(int x) {
        // set x to the value of number: 10
        super.setX(number);
    }
}

public class Test {
    public static void main(String[] args) {
        B b = new B();
        // b.getX() should be 10, right?
        System.out.println("B.getX() = " + b.getX());
    }
}

The output of this program is:

B.getX() = 0

The reason is that B's number member is not initialized at the time setX is called, so its default value of 0 is used.

This article has a more thorough explanation, as does Effective Java.

这篇关于Java - 应该通过getters和setters方法在构造函数中访问私有实例变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆