super关键字

一、调用父类构造方法,必须在子类构造方法中的第一句,调用相应的构造方法,若不显示的写出来,默认调用父类的默认构造方法。例如:super()

import com.sun.org.apache.bcel.internal.generic.NEW;

        import javax.naming.Name;

/**
 * Created by 25037 on 2021/11/28.
 */
public class jicheng01 {
    public static void main(String[] args) {
        HomeDog homeDog = new HomeDog();
        homeDog.print("旺财");

    }

    static class Dog {//Dog作为父类
        protected String Name;
        private String Sex;

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

        public Dog(String Name,String Sex) {
            this.Name= Name;
            this.Sex=Sex;
            System.out.println("我是Dog-父类的构造方法");
        }
    }

    static class HomeDog extends Dog {//HomeDog继承Dog类

        public HomeDog() {
            super("旺财","公");
            System.out.println("我是HomeDog的构造方法");
        }

        //        继承一个父类只能继承非私有的属性和方法
        public void print(String Name) {
            this.Name=Name;
            System.out.println(Name + "我是一只家狗,wangwang");
        }
    }

    class HaSkyDog extends Dog {
        public HaSkyDog() {
            super("哈士奇","母");
            System.out.println("我是HaSkyDog的构造方法");
        }

        public void show() {
            System.out.println(Name + "我是一只哈士奇,我能跳舞");
        }
    }
}

二、super可以调用父类的属性,可以从父类实例处获得信息

import com.sun.org.apache.bcel.internal.generic.NEW;

        import javax.naming.Name;

/**
 * Created by 25037 on 2021/11/28.
 */
public class jicheng01 {
    public static void main(String[] args) {
        HomeDog homeDog = new HomeDog();
        homeDog.print("旺财");

    }

    static class Dog {//Dog作为父类
        protected String Name;
        private String Sex;

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

    static class HomeDog extends Dog {//HomeDog继承Dog类

        //        继承一个父类只能继承非私有的属性和方法
        public void print() {
        //调用父类的Name属性,此处可以省略super,因为Name可以从父类继承
            System.out.println(super.Name + "我是一只家狗,wangwang");
        }
    }

  
}

 三、可以调用父类的方法,可以委托父类方法帮助完成某件事

 

import com.sun.org.apache.bcel.internal.generic.NEW;

        import javax.naming.Name;

/**
 * Created by 25037 on 2021/11/28.
 */
public class jicheng01 {
    public static void main(String[] args) {
        HomeDog homeDog = new HomeDog();
        homeDog.print("旺财");

    }

    static class Dog {//Dog作为父类
        protected String Name;
        private String Sex;

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

       
    }

    static class HomeDog extends Dog {//HomeDog继承Dog类
         public void eat() {
            //重写父类eat方法候,也想使用父类的eat方法,那么可以使用super调用父类的eat方法
            super.eat();
            System.out.println("我是一只家狗,wangwang");
         }
       
    }

    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暇光曙墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值