JAVA小练习51——继承的练习

    xiaoxiao2022-07-03  125

    class Animal{ String name; private int age; String color; public Animal(String name,String color,int age){ this.name = name; this.color = color; this.age=age; System.out.println("Animal带参的构造函数..."); } public Animal(){ System.out.println("Animal无参的构造函数..."); } public void eat(){ System.out.println(color+name+"在吃..."); } } //鸟 鸟是属于动物中一种。 class Bird extends Animal{ // Bird 就称作为Animal的子类。 Animal 称作为Bird的父类、 超类、基类 public Bird(){ System.out.println("Bird无参的构造函数..."); } public void fly(){ System.out.println(name+"在飞翔..."); } } class Demo51 { public static void main(String[] args) { Bird b = new Bird(); b.name = "喜鹊"; b.color = "彩色"; b.eat(); new Bird(); new Bird(); } }
    最新回复(0)