继承下的构造方法
admin
2024-03-06 01:10:54
0

 52.继承下的构造方法

继承条件下构造方法的调用规则

  • 子类构造方法没有通过super显示调用父类的有参构造方法,也通过this.显示调用自身其他构造方法

系统默认调用父类的无参构造方法

  • 子类构造方法通过super显示调用父类的有参构造方法

执行父类相应构造方法,而不执行父类无参构造方法

  • 子类构造方法通过this显示调用自身的其他构造方法,在相应构造方法中应用以上两条规则

//父类,提取共性代码Pet类

public class Pet {
    //1.隐藏属性(添加private)
    //昵称,默认值是“无名氏”
    private String name="无名氏";

    //1.health属性不被用户访问到 2.程序编写过程中要能控制health的赋值--方法中
    /*if(this.health<0||this.health>100){
        System.out.println("请输入0-100的值!");
        this.health=60;
    }else{
        this.health=health;
    }*/
    //健康值,默认值是100,健康值在0-100之间,小于60为不健康
    private int health=100;
    //亲密度
    private int love=0;
    int age=5;


    public Pet(){
        System.out.println("父类无参构造方法");
    }
    public Pet(String name){
        this.name=name;
    }
    public Pet(String name,int health,int love){
        //this(name);  //this可调用本类的构造方法,且必须在第一行
        this.name=name;
        this.health=health;
        this.love=love;
        System.out.println("父类的带参构造方法");
    }

    public int getAge() {
        return age;
    }

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

    //2.添加属性的setter/getter方法(方法公开),并加入属性控制语句
    //setter:1.属性赋值。2.属性的操作(正确判断等)
    public void setHealth(int health){
        if(health<0||health>100){
            System.out.println("请输入0-100的值!");
            this.health=60;
            return;
        }
        this.health=health;
    }
    //getter:属性取值
    public int getHealth(){
        return this.health;
    }

    public String getName() {
        return this.name;
    }

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

    public int getLove() {
        return this.love;
    }

    public void setLove(int love) {
        if(love<0||love>100){
            System.out.println("请输入0-100的值!");
            this.love=60;
            return;
        }
        this.love=love;
    }

     /**
      * 输出宠物的信息
     * */
    public void print(){
        System.out.println("宠物的自白:\n我的名字叫"+this.name+",健康值为"+this.health+
                ",和主人的亲密度为"+this.love+"。");
//        if(this.health<0||this.health>100){
//            System.out.println("请输入0-100的值!");
//            this.health=60;
//        }else{
//            this.health=health;
//        }
    }
}

/*
* 宠物狗狗类Dog类
* */

import java.sql.SQLOutput;

/*
* 宠物狗狗类
* */
public class Dog extends Pet{

    //品种
    private String strain="聪明的拉布拉多犬";
    private int age=3;
    
    public Dog(){
        System.out.println("子类狗狗的无参构造方法");
    }
    public Dog(String name,int health,int love){
        super(name,health,love);
        System.out.println("子类狗狗带三个参数的构造方法");
    }
    public Dog(String name,int health,int love,String strain){
        /*//通过super调用父类的构造方法,必须是第一行
        //super();注意参数顺序必须和父类一致
        super(name,health,love);*/
        this(name,health,love);
        this.strain=strain;
        System.out.println("子类狗狗的带四个参数的构造方法");
    }


    public String getStrain() {
        return this.strain;
    }

    public void setStrain(String strain) {
        this.strain = strain;
    }

    public void print(){
        //调用父类的非private方法print()
        super.print();
        System.out.println(",我是一只:"+this.strain);
    }

    public void m1(){
        /*//super不可以调用父类的private属性
        System.out.println(super.name);*/
        //super可以调用父类的非private属性
        System.out.println(super.age);
    }

    public void m2(){
        //子类会覆盖父类的同名成员
        System.out.println(this.age);
        //可使用super调用父类被子类覆盖的同名成员
        System.out.println(super.age);
    }
}

/*
* 宠物企鹅类Penguin类
* */

public class Penguin extends Pet {

    //性别
    private String sex="Q仔";

    public String getSex() {
        return this.sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void print(){
        //调用父类的print()
        super.print();
        System.out.println(",我的性别是:"+this.sex);
    }
}

//宠物测试类TestPet类

import java.util.Scanner;

//宠物测试类
public class TestPet {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("欢迎来到宠物店!");
        System.out.println("请输入您要领养宠物的名字:");
        String name = input.next();
        System.out.println("请输入您要领养的宠物类型:1.狗狗 2.企鹅");
        int typeNo = input.nextInt();
        switch (typeNo) {
            case 1:
                //接受用户键盘输入值
                System.out.println("请输入宠物的健康值:");
                int ghealth = input.nextInt();
                System.out.println("请输入宠物与主人的亲密度:");
                int glove = input.nextInt();
                System.out.println("请输入宠物的品种:");
                String strain = input.next();

                /*//创建狗狗对象,并为狗狗属性赋值
                Dog dog = new Dog();
                dog.setName(name);
                //dog.health=-1000;
                dog.setHealth(ghealth);
                //System.out.println(dog.getHealth());
            */
              /*  //伪代码
            do{
                dog.health=-1000;
                System.out.println("请输入0-100之间的数值!");
            }while(dog.health>0||dog.health<100);
            dog.love=3;
            dog.name="多多";
            dog.strain="吉娃娃";
                dog.setLove(glove);
                dog.setStrain(strain);
                dog.print();
                //dog.m1();*/
                Dog dog=new Dog("狗蛋",244,23,"二哈");
                dog.print();
                dog.m2();
                break;
            case 2:
                //接受用户键盘录入值
                System.out.println("请选择宠物的性别:1.Q妹 2.Q仔");
                int sexId = input.nextInt();
                String sex = (sexId == 1) ? "Q妹" : "Q仔";
                System.out.println("请输入宠物的健康值:");
                int qhealth = input.nextInt();
                System.out.println("请输入宠物和主人的亲密度:");
                int qlove = input.nextInt();

                //创建企鹅对象,并为企鹅属性赋值
                Penguin p = new Penguin();
            /*p.health=-1000;
            p.love=3;
            p.name="Q仔";
            p.sex="男";*/
                p.setName(name);
                p.setHealth(qhealth);
                p.setLove(qlove);
                p.setSex(sex);
                p.print();
                break;
            default:
                System.out.println("暂时没有这个类型的宠物,请在1或者2之间选择数值输入!");
                break;
        }
    }
}

相关内容

热门资讯

吹风机养生:真能顶得上老中医吗... 近年来,社交平台上流行起了“吹风机养生”的说法,许多博主宣称用吹风机对着特定的穴位吹热风,可以温阳驱...
深夜王炸,GPT-5.5发布 财联社 美东时间周四,OpenAI公布了其最新的人工智能模型——GPT-5.5。该公司表示,该模型在...
台湾同胞参加海军成立77周年纪... 4月23日,台湾同胞登上海军舰艇参观并与海军军官合影留念。新华社记者 李杰 摄 新华社青岛4月23日...
特朗普:这世界疯了 美国司法部当地时间4月23日证实,参与强行控制并转移委内瑞拉总统马杜罗的美国陆军特种部队军士长甘农·...
从冲击千亿市值到被申请破产重整... 图/ic 八年亏损超84亿元!“中国影视第一股”华谊兄弟被申请重整。 4月23日晚,华谊兄弟传媒股份...
“保安与停车女子和好接吻”,A... 撰稿/苏士仪(媒体人) 编辑/王言虎 校对/王心 ▲女司机与保安发生肢体冲突一事,持续引发关注。图...
美伊冲突前景不明 国际油价再度... 当地时间4月24日,由于美国与伊朗冲突前景仍不明朗,市场避险情绪升温,国际油价再度上涨。布伦特原油上...
美国发生群体性枪击事件,8名1... 新华社消息,美国路易斯安那州什里夫波特市19日发生一起群体性枪击事件,造成8名儿童死亡,枪手被警方击...
274亿美金砸向核武器,威慑还... 文︱陆弃 能源部长克里斯·赖特在2026年4月16号的一场公开场合称,美国正在推进的核武器现代化计划...