继承-------Super

super注意点:

1.super调用父类的构造方法,必须在构造方法的第一个

2.super 必须只能出现在子类的方法或者构造方法中!

3.super 和this 不能同时调用构造方法!

public class Person {
    //父类的无参构造
    public Person(){
        System.out.println("爸爸来啦");
    }
    
    String name = "李航";
    private int age = 10;//私有属性子类无法调用
    
    public void say(){
        System.out.println("我是李航啊");
    }
}


public class Student extends Person {
    //无参构造,先执行父类的无参构造,再执行子类的无参构造
    public Student() {
        super();//继承父类的无参构造
        System.out.println("儿砸来啦");
    }

    //定义子类属性name 和方法say()
    String name = "娣娣";
    public void say(){
        System.out.println("我是娣娣呀");
    }


    //test1----- super调用父类的属性,this 调用子类的属性
    public void test1 (String name){
        System.out.println(name);//这里的name指的是test1方法中的输入name
        System.out.println(this.name);
        System.out.println(super.name);
    }

    //test2----super调用父类的方法,this调用子类的方法
    public void test2(){
        say();
        this.say();
        super.say();
    }
}

/*
public class Amplication {
    public static void main(String[] args) {

        Student student = new Student();

        student.test1("点点");
        student.test2();
    }
}
*/

 

VS this :

代表的对象不同:

this :本身调用者这个对象

super :代表父类对象的应用

前提: this:没有继承也可以使用

super:只能在继承条件才可以使用

构造方法:

this() : 本类的构造

super():父类的构造

 

posted on 2022-08-27 10:29  三岁学JAVA  阅读(39)  评论(0)    收藏  举报