super_父类

package projict05;
/*
* 1.super
* 意思:父类
* 通过super访问的属性,方法或者构造器必须是在父类中可见的,不能是private,如果挎包,不能缺省
* super.属性:子类调用父类被重写的属性需要加super
* super.方法:子类调用父类被重写的方法时需要加super
* super():调用父类的无参构造
* super(实参列表):调用父类的有参构造
*/
public class test20 {
    public static void main(String[] args) {
        Son s=new Son();
        s.print();
        s.eat();

    }
    

}

class Father{
    String name="father";
    double weight=100.5;
    public void eat() {
        System.out.println("father eating");
    }
    public void sleep() {
        System.out.println("father sleeping");
    }
}

class Son extends Father{
    String name="son";
    int age=20;
    public void print() {
        System.out.println(super.name);//调用父类被重写的属性
        System.out.println(weight);//调用父类的普通属性

        System.out.println(name);//就近原则
        System.out.println(age);//就近原则
    }
    
    public void eat() {
        super.eat();//调用父类被重写的方法
        sleep();//调用父类普通方法
        System.out.println("son eating");
    }
}

猜你喜欢

转载自www.cnblogs.com/hapyygril/p/12929518.html