1 class TiXing{ 2 float up,height; 3 static float down; 4 5 TiXing(float x,float y,float z){ 6 up=x; 7 height=y; 8 down=z; 9 } 10 } 11 12 public class ep3_9{ 13 public static void main(String args[]){ 14 TiXing one=new TiXing(1,2,3); 15 System.out.println("one's down is:"+one.down); 16 TiXing two=new TiXing(4,5,6); 17 System.out.println("one's down is:"+one.down); 18 System.out.println("two's down is:"+two.down); 19 20 System.out.println("TiXing's down is:"+TiXing.down); 21 } 22 }
實例方法和類方法對實例變量和類變量的訪問html
實例方法能夠對當前對象的實例變量進行操做,也能夠對類變量進行操做。實例方法由實例對象調用。ide
1 class TiXing{ 2 private float up,height; 3 private static float down; 4 5 TiXing(float x,float y,float z){ 6 up=x; 7 height=y; 8 down=z; 9 } 10 public void display(){ 11 System.out.println("up is:"+up+"height is:"+height+"down is:"+down); 12 } 13 public static void change(float number){ 14 down=number; 15 //System.out.println("height:"+height);//出錯 16 } 17 } 18 19 public class ep3_9{ 20 public static void main(String args[]){ 21 TiXing one=new TiXing(1,2,3); 22 one.display(); 23 TiXing two=new TiXing(4,5,6); 24 one.display(); 25 two.display(); 26 27 //TiXing.display();//出錯 28 one.change(101); 29 one.display(); 30 two.change(102); 31 two.display(); 32 } 33 }
注:此文轉自http://www.cnblogs.com/scf141592/p/5726347.html,轉載請註明做者出處。this