引用变量.(变量)的使用

public class Puzzle4 {
    public static void main(String[] args){
        Puzzle4b[] obs=new Puzzle4b[6];
        int y=1;
        int x=0;
        int result =0;
        while(x<6){
            obs[x]=new Puzzle4b();
            obs[x].ivar=y; **#有一个引用变量.ivary**
            y=y*10;
            x=x+1;
        }
        x=6;
        while(x>0){
            x=x-1;
            result =result+obs[x].doStuff(x);
        }
        System.out.println("result"+ result);
    }
}

调用方法

public class Puzzle4b {
    int ivar;  //**分析发现int ivar=n(n为任意整数,不影响最终结果)。说明ivar 的值由public class Puzzle4{}中直接传递过来,实际上obs[x].ivar=y表示把y的值传递给ivar**
    public int doStuff(int factor){
        if(ivar>100){
            return ivar*factor;  //factor的值就是x的值
        }
        else{
            return ivar*(5-factor);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/nsjlive/article/details/78586357