public class TransferProperty {
int i=47; //定义成员变量
public void call() { //定义成员方法call()
System.out.println("调用call()方法");
for (i=0;i<3;i++){
System.out.println(i+" ");
if (i==2){ //输出1 2
System.out.println("\n");
}
}
}
//构造函数的定义
public TransferProperty(){
}
public static void main(String[] args){
TransferProperty t1=new TransferProperty();//创建一个TransferProperty的对象
TransferProperty t2=new TransferProperty();//同上
t2.i=60; //将类成员变量赋值
System.out.println("第一个实例对象调用变量i的结果:"+t1.i++);
t1.call();//调用第一个对象调用类方法
System.out.println("第二个实例对象调用变量i的结果:"+t2.i);
t2.call();//使用第二个对象调用类成员方法
}
}
虽然使用两个对象调用同一个成员变量,但是结果不同
在输出之前将该值重新赋值为60,但是赋值时使用的是第二个对象t2调用成员变量,所以在第一个对象调用成员变量的时候依然是成员变量的初始值
package com.FaceObject;
public class AccessProperty {
static int i=47; //定义成员变量
public void call() { //定义成员方法call()
System.out.println("调用call()方法");
for (i=0;i<3;i++){
System.out.println(i+" ");
if (i==2){ //输出1 2
System.out.println("\n");
}
}
}
//构造函数的定义
public AccessProperty(){
}
public static void main(String[] args){
AccessProperty t1=new AccessProperty();//创建一个TransferProperty的对象
AccessProperty t2=new AccessProperty();//同上
t2.i=60; //将类成员变量赋值
System.out.println("第一个实例对象调用变量i的结果:"+t1.i++);
t1.call();//调用第一个对象调用类方法
System.out.println("第二个实例对象调用变量i的结果:"+t2.i);
t2.call();//使用第二个对象调用类成员方法
}
}
由于使用t2.i=60语句改变了静态成员变量的值, 使用t1调用时静态成员变量的值也为60,这就是i被定义为static的效果
**即使使用两个对象对同个静态成员变量进行操作,依然可以改变静态成员变量的值**
*因为内存中两个对象指向的都是同块内存区域* 当t1.i++执行完毕之后,i值变成3.、 当再次调用时call()方法又被重新赋值为0,做循环打印操作。