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();}}

Java 接口实现飞行功能:鸟类和昆虫类示例

原文地址: https://www.cveoy.top/t/topic/pUmY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录