企鹅,老鼠:属性(姓名,id),方法(吃,睡,自我介绍)输出两个动物的结果,(java的继承的练习)

企鹅:属性(姓名,id),方法(吃,睡,自我介绍)Penguin
老鼠:属性(姓名,id),方法(吃,睡,自我介绍)Mouse
并输出两个动物的结果;


解:
package com.zy;
/*子类
 * 企鹅:属性(姓名,id),方法(吃,睡,自我介绍)
 */
public class Penguin extends Animal {
    public Penguin(String myName, int myid) {
        super(myName, myid);
    }
}
package com.zy;
/* 子类
 * 老鼠:属性(姓名,id),方法(吃,睡,自我介绍)Mouse
 */
public class Mouse extends Animal {
    public Mouse(String myName, int myid) {
        super(myName, myid);
    }
    }
package com.zy;
/*
 * 父类
 */
public class Animal {
    String name;
    int id;
    public Animal(String myName, int myid) {
            name = myName;
            id = myid;
       }
    public void eat() {
        System.out.println(name+"在吃饭");
    }
    public void sleep() {
        System.out.println(name+"在睡觉");
    }
    public void introduction() {
        System.out.println("我是"+name+"\t"+"编号"+id);
    }
}
测试类 :
package com.zy;
public class TestAnimal {
    public static void main(String[] args) {
        //创建实例化对象
        Mouse mouse=new Mouse("老鼠一号", 46);
        mouse.introduction();
        mouse.sleep();
        mouse.eat();
        Penguin penguin =new Penguin("企鹅一号", 25);
        penguin.introduction();
        penguin.sleep();
        penguin.eat();
    }
}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Java代码实现: ```java //动物类 public class Animal { private String name; private int month; private String species; public Animal(String name, int month, String species) { this.name = name; this.month = month; this.species = species; } public void eat() { System.out.println(name + " is eating."); } public void getAge() { System.out.println(name + " is " + month + " months old."); } public String getSpecies() { return species; } } //狗类 public class Dog extends Animal { private double weight; private String bark; public Dog(String name, int month, String species, double weight, String bark) { super(name, month, species); this.weight = weight; this.bark = bark; } public void setBark(String bark) { this.bark = bark; } public void getWeight() { System.out.println(getSpecies() + " " + getName() + "'s weight is " + weight + "kg."); } public void setWeight(double weight) { this.weight = weight; } public void setShape(String shape) { System.out.println(getSpecies() + " " + getName() + "'s house shape is " + shape); } } //企鹅类 public class Penguin extends Animal { private double weight; public Penguin(String name, int month, String species, double weight) { super(name, month, species); this.weight = weight; } public void run() { System.out.println(getName() + " is running."); } public void getWeight() { System.out.println(getSpecies() + " " + getName() + "'s weight is " + weight + "kg."); } public void setWeight(double weight) { this.weight = weight; } } //测试类 public class Test { public static void main(String[] args) { Dog dog = new Dog("Tom", 12, "Golden Retriever", 25.6, "Wang Wang"); dog.eat(); dog.getAge(); dog.getWeight(); dog.setShape("Round"); Penguin penguin = new Penguin("Jerry", 6, "Emperor Penguin", 10.3); penguin.eat(); penguin.getAge(); penguin.getWeight(); penguin.run(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值