p160页编程 使用接口实现动物模拟器
2017-10-28 17:06
477 查看
题目:
设计一个动物模拟器,模仿动物的叫声。
Animal为接口类,里面有两个抽象方法cry()和getAnimalName()
模拟器类Simulator里面有一个palySound(Animal animal)方法,参数animal重写Animal类中的cry()和getAnimalName();
Dog类和Cat类用于实现接口
主类Aplication运行程序
package 动物模拟器2;
public interface Animal {
public abstract void cry() ;
public abstract String getAnimalName();
}
package 动物模拟器2;
public class Dog implements Animal {
public String getAnimalName() {
System.out.println("大黄狗");
return "大黄狗";
}
public void cry() {
System.out.println("汪汪汪汪!!!");
}
}
package 动物模拟器2;
public class Cat implements Animal {
public String getAnimalName() {
System.out.println("小花猫");
return "小花猫";
}
public void cry() {
System.out.println("喵喵喵喵!!!");
}
}
package 动物模拟器2;
public class Simulator {
void playsound(Animal animal) {
animal.getAnimalName();
animal.cry();
}
}
package 动物模拟器2;
public class Application {
public static void main(String args[]) {
Simulator simulator=new Simulator();
simulator.playsound(new Dog());
simulator.playsound(new Cat());
}
}
运行结果:
设计一个动物模拟器,模仿动物的叫声。
Animal为接口类,里面有两个抽象方法cry()和getAnimalName()
模拟器类Simulator里面有一个palySound(Animal animal)方法,参数animal重写Animal类中的cry()和getAnimalName();
Dog类和Cat类用于实现接口
主类Aplication运行程序
package 动物模拟器2;
public interface Animal {
public abstract void cry() ;
public abstract String getAnimalName();
}
package 动物模拟器2;
public class Dog implements Animal {
public String getAnimalName() {
System.out.println("大黄狗");
return "大黄狗";
}
public void cry() {
System.out.println("汪汪汪汪!!!");
}
}
package 动物模拟器2;
public class Cat implements Animal {
public String getAnimalName() {
System.out.println("小花猫");
return "小花猫";
}
public void cry() {
System.out.println("喵喵喵喵!!!");
}
}
package 动物模拟器2;
public class Simulator {
void playsound(Animal animal) {
animal.getAnimalName();
animal.cry();
}
}
package 动物模拟器2;
public class Application {
public static void main(String args[]) {
Simulator simulator=new Simulator();
simulator.playsound(new Dog());
simulator.playsound(new Cat());
}
}
运行结果:
相关文章推荐
- p160页编程 使用接口实现动物模拟器
- 编程作业160 页 第5题 使用(接口)设计一个动物声音"模拟器",希望模拟器可以模拟许多动物声音
- 动物声音“模拟器”(接口与实现)
- 面向接口编程时不能使用java已经实现的类的
- 用接口实现动物声音模拟器
- mybatis系列六:使用getMapper方式实现面向接口的编程
- Java使用简单工厂模式对面向接口编程模式的深度解耦实现
- 用接口实现动物声音模拟器
- Java使用简单工厂模式对面向接口编程模式的深度解耦实现
- 使用函数接口和枚举实现配置式编程(Java与Scala实现)
- Java语言 设计一个动物声音“模拟器”(用接口实现)
- 在J2ME编程中使用Nokia的中文模拟器
- Office with .Net (二) ――― 使用.Net访问Office编程接口
- 使用MFC快速实现网络编程
- 针对接口编程,而非(接口的)实现
- 使用接口实现功能扩展
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- 在C#中使用异步Socket编程实现TCP网络服务
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- Office with .Net (二) ――― 使用.Net访问Office编程接口