编写程序要求如下: 1定义Biology生物、Animal动物和Mankind人3个接口; 2接口Biology声明breath抽象方法; 3接口Animal继承Biolog并声明move和eat抽象方法; 4接口Mankind继承Animal并声明study和think抽象方法; 5定义类NormalMan实现接口Mankind仅显示相应的功能信息来实现它们声明的抽象方法; 6在类NormalM
以下是代码实现:
// 定义Biology(生物)接口 interface Biology { void breath(); }
// 定义Animal(动物)接口,继承自Biology interface Animal extends Biology { void move(); void eat(); }
// 定义Mankind(人)接口,继承自Animal interface Mankind extends Animal { void study(); void think(); }
// 定义NormalMan类,实现Mankind接口 class NormalMan implements Mankind {
private String name;
// 无参构造方法
public NormalMan() {
name = "Unknown";
}
// 有参构造方法
public NormalMan(String name) {
this.name = name;
}
// 实现Mankind接口中的抽象方法
public void study() {
System.out.println(name + " is studying.");
}
public void think() {
System.out.println(name + " is thinking.");
}
public void move() {
System.out.println(name + " is moving.");
}
public void eat() {
System.out.println(name + " is eating.");
}
public void breath() {
System.out.println(name + " is breathing.");
}
}
// 测试类 public class InterfaceTest { public static void main(String[] args) { NormalMan man = new NormalMan("Tom"); man.study(); man.think(); man.move(); man.eat(); man.breath(); } }
原文地址: http://www.cveoy.top/t/topic/bspQ 著作权归作者所有。请勿转载和采集!