你好
静态是优先于对象存在的所以类名调用静态成员的时候是调用引用所属类的静态成员变量或方法。- class Person
- {
- public static int a=1;
- public static void eat()
- {
- System.out.println("person eat");
- }
- }
- class Student extends Person
- {
- public static int a=2;
- public static void eat()
- {
- System.out.println("student eat");
- }
- }
- public class Test
- {
- public static void main(String[] args) throws Exception
- {
- Person p = new Student();
- System.out.println(p.a);
- p.eat();
- }
- }
复制代码 打印结果是:
1
person eat |