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