java处理对象中的数据

class Father {
    int age = 50;
    static int score = 75;

    public void show() {
        System.out.println("这是老爸的show");
    }
}

class Son extends Father {
    int age = 25;
    static int score = 100;

    public void show() {
        System.out.println("这是儿子的show");
    }
}

class Demo1 {
    public static void main(String[] args) {
        Father f = new Son();
        System.out.println(f.age);
        f.show();

    }

}

上面是一段看上去没有什么内容好说的,但他包含了java处理对象中的数据问题:
它处理数据时有两个过程,当然是编译和运行,Father f = new Son();在处理这个代码时
成员变量,静态变量,静态方法编译和运行都看左边,就是都以父类为主。成员方法编译看左边,其实看左边也就是说你父类没有show方法也会报错,运行看右边。
如果在子类的方法中去找变量的话先去找子类中的再去找父类中的。和这种直接在main方法中取值不要搞混了。

你可能感兴趣的:(java处理对象中的数据)