java程序

(1)编写一个抽象类Animal,其成员变量有name,age,weight表示动物名、年龄和重量。

方法有showInfo( )、move( )和eat( ),其中后面两个方法是抽象方法。

(2)编写一个类Bird继承Animal,实现相应的方法。通过构造方法给name,age,weight分别赋值,showInfo( )打印鸟名、年龄和重量,move( )方法打印鸟的运动方式,eat( )打印鸟喜欢吃的食物。

(3)编写测试类TestAnimal,用Animal类型的变量,调用Bird对象的三个方法。

Animal抽象类。

public abstract class Animal {

public String name;

public int age;

public float weight;

public void showInfo(){
System.out.println("鸟名为:" + this.name + "\r年龄为:" + this.age + "\r体重为:" + this.weight);
}

public abstract void move();

public abstract void eat();
}

Bird子类,继承自Animal
public class Bird extends Animal {
@Override
public void move() {
// TODO Auto-generated method stub
System.out.println("鸟用跳的。");
}
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("我也不知道鸟喜欢吃什么- -#");
}
public Bird(String name,int age,float weight) {
super();
super.name = name;
super.age = age;
super.weight = weight;
}
}

测试类,使用Junit框架
import org.junit.Test;
import junit.framework.TestCase;
public class TestUseCase extends TestCase {
@Test
public void testShowInfo(){
Bird b = new Bird("小鸟", 2, 66);
b.showInfo();
}
@Test
public void testMove(){
Bird b = new Bird("小鸟", 2, 66);
b.move();
}
@Test
public void testEat(){
Bird b = new Bird("小鸟", 2, 66);
b.eat();
}
}

运行结果如下,控制台将打印:
鸟名为:小鸟
年龄为:2
体重为:66.0
鸟用跳的。
我也不知道鸟喜欢吃什么- -#
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-09-30
相似回答