继承 super详解

继承

super详解

package oop;


//import oop.demo04.Student;
import oop.D5.Student;


public class Application {
    public static void main(String[] args) {
//        Student s1 = new Student();
//
//        s1.setName("xc");
//        System.out.println(s1.getName());
//        s1.setAge(999);//不合法的
//        System.out.println(s1.getAge());
        Student student = new Student();
        student.test("xc666");

    }
}
package oop.D5;

public class Person {
    protected String name="xc";

    public void print(){
        System.out.println("Person");
    }

}
package oop.D5;

public class Student extends Person{
    private String name="许璨";
    public void test(String name){
        System.out.println(name);//xc666
        System.out.println(this.name);//许璨
        System.out.println(super.name);//xc
    }
    public void print(){
        System.out.println("Student");
    }
    public void test1(String name){
       print();//Student
       this.print();//Student
       super.print();//Person
    }

}

super注意点:

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

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

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

和this对比:

代表的对象不同:

this:本身调用者这个对象

super:代表父类对象的应用

前提

this:没有继承也可以使用

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

构造方法:

this():本类的构造

super():父类的构造!

posted @ 2020-12-17 22:03  XC666  阅读(157)  评论(0)    收藏  举报