访问对象的属性和行为

1、

public class TransferProperty {
    int i = 47;
    public void call() {
        System.out.println("调用call()方法");
        for(i=0;i<3;i++) {
            System.out.print(i + " ");
            if(i==2) {
                System.out.println("\n");
            }
        }
    }
    
    public TransferProperty() {}
    
    public static void main(String[] args) {
        TransferProperty t1 = new TransferProperty();
        TransferProperty t2 = new TransferProperty();
        t2.i = 60;
        System.out.println("第一个实例对象t1调用变量i的结果:" + t1.i++);
        t1.call();
        System.out.println("第二个实例对象t2调用变量i的结果:" + t2.i);
        t2.call();
    }
}

运行结果:

第一个实例对象t1调用变量i的结果:47
调用call()方法
0 1 2 
第二个实例对象t2调用变量i的结果:
60 调用call()方法 0 1 2

2、

public class TransferProperty {
    static int i = 47;
    public void call() {
        System.out.println("调用call()方法");
        for(i=0;i<3;i++) {
            System.out.print(i + " ");
            if(i==2) {
                System.out.println("\n");
            }
        }
    }
    
    public TransferProperty() {}
    
    public static void main(String[] args) {
        TransferProperty t1 = new TransferProperty();
        TransferProperty t2 = new TransferProperty();
        t2.i = 60;
        System.out.println("第一个实例对象t1调用变量i的结果:" + t1.i++);
        t1.call();
        System.out.println("第二个实例对象t2调用变量i的结果:" + t2.i);
        t2.call();
    }
}

运行结果:

第一个实例对象t1调用变量i的结果:60
调用call()方法
0 1 2 

第二个实例对象t2调用变量i的结果:3
调用call()方法
0 1 2 

猜你喜欢

转载自www.cnblogs.com/sunzhongyu008/p/11284331.html