public class Animal {
String name = "Animal name";
static String NAME = "ANIMAL NAME";
public static void RUN(){
System.out.println("this is Animal static RUN .");
}
public void run(){
System.out.println("this is Animal run .");
}
}
2、子类(Dog)
String name = "Dog name";
static String NAME = "DOG NAME";
int eye = 2;
public static void RUN(){
System.out.println("this is Dog static RUN .");
}
public void run(){
System.out.println("this is Dog run .");
}
3、测试
public static void main(String[] args) {
Animal dog = new Dog();
/**
* 变量与静态方法与声明的类绑定
*/
System.out.println(dog.name); // Animal name
System.out.println(dog.NAME); // ANIMAL NAME
System.out.println(dog.eye); // 报错,访问不到
dog.RUN(); // this is Animal static RUN .
/**
* 实例方法与定义的类绑定
*/
dog.run(); // this is Dog run .
}