Java 面向对象 之 super 关键字

http://www.verejava.com/?id=17159596599630java

/**

    this: 表明当前类的引用
        1. 当局部变量和成员变量同名时,  成员变量要加 this 限定
        2. 实例化时 能够用 this 调用当前类的构造方法,   必须写在第一行
        3. 能够用 this  调用当前类的 普通方法

    super : 表明当前父类的引用
        1. 实例化子类时,  能够用  super 调用父类的 非私有方法
        2. 实例化子类时.    能够用 super 调用父类的  构造方法 ,  必须写在第一行
        3. 在子类的方法中 , 能够用 supe 调用父类的 非私有方法.
*/
public class Test1 {

    public static void main(String[] args) {
        // 实例化 农夫

        Father father = new Father();
        father.setName("农夫");
        father.setAge(90);
        System.out.println(father.getAge() + " 岁 " + father.getName() + " 有 " + father.getWealth());

        Father father = new Father("农夫", 90);
        System.out.println(father.getAge() + " 岁 " + father.getName() + " 有 " + father.getWealth());

        Son son = new Son("农夫", 90);
        System.out.println("儿子知道父亲的 : " + son.getAge() + " 岁 " + son.getName() + " 有 " + son.getWealth());
        son.work();
    }
}

//父类
class Father {
    
    private String name;
    private int age;
    private String wealth;

    public Father() {
        wealth = "100两黄金";
    }
相关文章
相关标签/搜索