标签:
class TiXing{ float up,height; static float down; TiXing(float x,float y,float z){ up=x; height=y; down=z; } } public class ep3_9{ public static void main(String args[]){ TiXing one=new TiXing(1,2,3); System.out.println("one‘s down is:"+one.down); TiXing two=new TiXing(4,5,6); System.out.println("one‘s down is:"+one.down); System.out.println("two‘s down is:"+two.down); System.out.println("TiXing‘s down is:"+TiXing.down); } }
实例方法和类方法对实例变量和类变量的访问
实例方法可以对当前对象的实例变量进行操作,也可以对类变量进行操作。实例方法由实例对象调用。
类方法不能访问实例变量,只能访问类变量。类方法由类名或者实例对象调用。类方法中不能出现this或者super关键字
class TiXing{ private float up,height; private static float down; TiXing(float x,float y,float z){ up=x; height=y; down=z; } public void display(){ System.out.println("up is:"+up+"height is:"+height+"down is:"+down); } public static void change(float number){ down=number; //System.out.println("height:"+height);//出错 } } public class ep3_9{ public static void main(String args[]){ TiXing one=new TiXing(1,2,3); one.display(); TiXing two=new TiXing(4,5,6); one.display(); two.display(); //TiXing.display();//出错 one.change(101); one.display(); two.change(102); two.display(); } }
JAVA类与对象(六)------实例变量与类变量的区别,实例方法和类方法的区别
标签:
原文地址:http://www.cnblogs.com/scf141592/p/5726347.html