要求设置一个我能飞行的接口一个鸟类一个昆虫类 java实现打印我是鸽子我有2只腿我可以生蛋、我可以飞; 打印我是大雁我有2只腿我可以生蛋、我可以飞; 打印我是蚂蚁我有6只腿我可以产卵、我可以飞; 打印我是蜜蜂我有6只腿我可以产卵、我可以飞;
public interface Flyable {
void fly();
}
public class Bird {
private String name;
public Bird(String name) {
this.name = name;
}
public void printInfo() {
System.out.println("我是" + name + ",我有2只腿,我可以生蛋,我可以飞");
}
}
public class Insect {
private String name;
private int legCount;
public Insect(String name, int legCount) {
this.name = name;
this.legCount = legCount;
}
public void printInfo() {
System.out.println("我是" + name + ",我有" + legCount + "只腿,我可以产卵,我可以飞");
}
}
public class Main {
public static void main(String[] args) {
Bird pigeon = new Bird("鸽子");
pigeon.printInfo();
Bird goose = new Bird("大雁");
goose.printInfo();
Insect ant = new Insect("蚂蚁", 6);
ant.printInfo();
Insect bee = new Insect("蜜蜂", 6);
bee.printInfo();
}
}
``
原文地址: http://www.cveoy.top/t/topic/ibF4 著作权归作者所有。请勿转载和采集!