javaI编写程序要求:1. 编写一个接口Animal,其成员变量有name,age,weight表示动物名、年龄和重量。声明方法showInfo( )、move( )和eat( )。 2. 编写一个类Bird实现Animal相应的方法。通过构造方法给name,age,weight分别赋值,showInfo( )打印鸟名、年龄和重量,move( )方法打印鸟的运动方式,eat( )打印鸟喜欢吃的食物。 3. 编写测试类TestAnimal,用Animal类型的变量,调用Bird对象的三个方法。
时间: 2023-06-25 12:03:59 浏览: 220
有关于java程序编程的一些基本
以下是 Java 代码实现:
Animal.java
```java
public interface Animal {
String name = "";
int age = 0;
double weight = 0.0;
void showInfo();
void move();
void eat();
}
```
Bird.java
```java
public class Bird implements Animal {
private String name;
private int age;
private double weight;
public Bird(String name, int age, double weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
@Override
public void showInfo() {
System.out.println("Bird name: " + this.name);
System.out.println("Age: " + this.age);
System.out.println("Weight: " + this.weight + " kg");
}
@Override
public void move() {
System.out.println("Fly");
}
@Override
public void eat() {
System.out.println("Insects");
}
}
```
TestAnimal.java
```java
public class TestAnimal {
public static void main(String[] args) {
Animal bird = new Bird("Sparrow", 1, 0.1);
bird.showInfo();
bird.move();
bird.eat();
}
}
```
输出结果:
```
Bird name: Sparrow
Age: 1
Weight: 0.1 kg
Fly
Insects
```
阅读全文