我可能有一个愚蠢的问题,例如,我想从方法访问本地属性:

public class Example {
    private int myprop;
    public int getMyprop() {
        return myprop;
    }

    public void setMyprop(int myprop) {
        this.myprop= myprop;
    }

    public void useProperty(){
    // i want to use here the variable: 'myprop' how i can accomplish this?
    }
}


谢谢你的时间。

最佳答案

getMyprop()中的操作相同:按名称。

public void useProperty(){
    if (myprop == 42) {
        System.out.println("It's the Answer to the Ultimate Question of Life, the Universe, and Everything");
    }
}

10-08 14:38