标签:实例 bsp ati [] 方法 nbsp var rgs ble
import java.util.Enumeration; class Dog{ static int legNum; int age; public static void walk(){ //eat(); //不能在类方法中访问实例变量 legNum = 4; //类方法访问类变量 System.out.println(legNum+"条腿走路!"); //age=6; //不能在类方法中访问实例变量 } public void eat(){ walk(); //可以在实例方法中访问类方法 age = 6; //实例变量只能在实例方法中访问; System.out.print("狗狗"+age+"岁了!"); System.out.println(legNum+"条腿走路!");//可以在实例方法中访问类属性 } } public class TestStaticVariable { public static void main(String[] args) { Dog.walk();//walk方法用类名直接访问 Dog huanhuan = new Dog(); huanhuan.eat(); //eat方法必须通过实例对象名huanhuan才能访问 } }
运行结果:
4条腿走路!
4条腿走路!
狗狗6岁了!4条腿走路!
标签:实例 bsp ati [] 方法 nbsp var rgs ble
原文地址:http://www.cnblogs.com/Timenow/p/6852163.html