super调用父类的属性方法

super:可以用来修饰属性  方法   构造器

             当子类与父类中有同名的属性时,可以通过   super.此属性  显式的调用父类声明的属性

             若想调用子类的同名的属性“this.此属性”

2.当子类重写父类的方法以后,在子类中若想再显式的调用父类的被重写的方法,就需要使用 super.方法

3.super修饰构造器:通过在子类中使用“super(形参列表)”来显示的调用父类中指定的构造器

                   >在构造器内部,super(形参列表) 必须声明在首行

                   >在构造器内部,this(形参列表)或super(形参列表)  只能出现一个

                   >当构造器中,不是显示调用this(形参列表) 或super(形参列表)其中任何一个默认调用的是父类空参的构造器

建议:当设计一个类时,尽量要提供一个空参的构造器。

Person:

package com.aff.sup;

public class Person {
    protected String name;
    private int age;
    int id = 1001;// 身份证号

    public Person() {
        this.name = "AA";
        this.age = 1;
    }

    public Person(String name) {
        this();
        this.name = name;
    }

    public Person(String name, int age) {
        this(name);
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void eat() {
        System.out.println("eating");
    }

    public void sleep() {
        System.out.println("sleeping");
        this.eat();
    }

    // 比较当前对象与形参的对象的age谁大
    public int compare(Person p) {
        if (this.age > p.age) {
            return 1;
        } else if (this.age < p.age) {
            return -1;
        } else {
            return 0;
        }
    }
}

Student:

package com.aff.sup;

public class Student extends Person {
    private String schoolName;
    int id = 1002;// 学号

    public Student() {
        super();
    }
    
    public void  eat(){
        System.out.println("学生吃饭");
    }
    
    public void info(){
        this.eat();
        super.eat();
        super.sleep();//写成this.sleep也行 反正只有一个
    }

    public void show() {
        System.out.println(this.id);// 1002
        System.out.println(super.id);// 1001 调用父类的id
        System.out.println(super.name);
    }
}

TestStudent:

package com.aff.sup;

public class TestStudent {
    public static void main(String[] args) {
        Student s = new Student();
        s.show();
        s.info();
    }
}

输出结果:

1002
1001
AA
学生吃饭
eating
sleeping
学生吃饭

 
All that work will definitely pay off
原文地址:https://www.cnblogs.com/afangfang/p/12516436.html