super注意点:

1.super调用父类构造方法,必须在构造方法第一个
2.super必须只能出现子类方法或者构造方法中!
3.superthis不能同时调用构造方法

Vs this

代表对象不同this:本身调用者这个对象
    super代表父类对象的应用
前提
    this:没有继承可以使用
    super:只能在继承条件可以使用
构造方法
    this();本类构造
    super():父类构造
/**
 * @Description super详解
 */
package com.oop;

import com.oop.demo05.Student;

public class Application {

    public static void main(String[] args) {

        Student student = new Student();
        //student.test("AI福");
        //student.test1();

    }

}

/**
 * @Description super详解
 */
package com.oop.demo05;

//在java中,所有的类,都默认直接或者间接继承object
//Person 人:父类
public class Person /*extends Object*/{

    public Person(String name) {
        System.out.println("Person无参执行了");
    }

    protected String name = "Ai福";

    //私有的的东西无法被继承
    public void print(){
        System.out.println("Person");
    }

}

/**
 * @Description super详解
 */
package com.oop.demo05;

//学生 is 人:派生类子类
//子类继承父类,就会拥有父类的全部方法!
public class Student extends Person{

    public Student() {
        //隐藏代码super()  调用了父类的无参构造

        super("name");//调用父类的构造器,必须要在子类构造器的第一行
        System.out.println("Student无参执行了");
    }


    private String name = "ai福";

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

    public void test1(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }

    public void test(String name){
        System.out.println(name);   //AI福
        System.out.println(this.name);    //ai
        System.out.println(super.name);   //Ai
    }

}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注