继承初体验

// 继承关键字extends,一个子类只能对应一个父类,这就是继承的单一性
class Student2 extends Personer{
    String name;  // 没赋值是null
    public static int age = 18;
    public void study(){
        System.out.println(this.name + "在学习"); // 方法中的this指向类
    }
    public Student2() {
        // 由此可见super()虽然是默认调用(☆),但是不显示得写出来就不会和this()这样调用构造函数起冲突;
        // super()显示出来和this()只能出现一个,因为他们都需要在构造函数第一行的位置
        //
        this("李四");
    }
    public Student2(String name) {
        this.name = name;
    }
    // super也可在方法中,当然了静态了不行
    public void keyboards(){
        System.out.println("我是学生用的键盘" + super.name);
    }
}
class Personer{
    String name;

    public Personer() {
        System.out.println("我调用了父类的构造函数吗");
    }
}

 

posted @ 2021-02-15 10:44  火鸡的呐喊  阅读(18)  评论(0编辑  收藏  举报