本文主要是介绍用java实现抽象类门Door,能进行开门(openDoor)关门(closeDoor)操作,有防盗,防水,防火,防锈,防弹功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用java实现抽象类门Door,能进行开门(openDoor)关门(closeDoor)操作,有防盗,防水,防火,防锈,防弹功能
1.背景
前两天老师布置了这个作业,自己正好趁着这次机会,把已经学过的java知识:继承,重写,抽象,接口,上转型对象,异常类以及Class类等整合一下。
2.题目
做一个门类Door,该门能进行开门openDoor与关门closeDoor操作。门再分各种功能,有防盗的,防水的,防火的,防锈的,防弹的。试使用java语言解决此问题。
3.分析
很显然,类Door可以抽象出来,其具有抽象行为openDoor和closeDoor。至于门的各种功能可以做成功能接口,根据题意可以写5个接口:防盗TheftProof,防水WaterProof,防火FireProof,防锈RustProof,防弹BulletProof。抽象的类Door,可以根据现实派生出四种档次的门:初级档次门(防盗),中级档次门(防盗,防水,防火),高级档次门(防盗,防水,防火,防锈),特级档次门(防盗,防水,防火,防锈,防盗)。分析已经完成,接下类就用代码实现它吧
4.具体代码(附注释)
(1)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public abstract class Door { // 抽象类Door门,应具有开关门功能public abstract void openDoor(); // 开门public abstract void closeDoor(); // 关门
}
(2)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public interface TheftProof { // 防盗功能接口public String showTheftProof(); // 显示防盗功能
}
(3)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public interface WaterProof { // 防水功能接口public String showWaterProof(); // 显示防水功能
}
(4)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public interface FireProof { // 防火功能接口public String showFireProof(); // 显示防火功能
}
(5)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public interface RustProof { // 防锈功能接口public String showRustProof(); // 显示防锈功能
}
(6)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public interface BulletProof { // 防弹功能接口public String showBulletProof(); // 显示防弹功能
}
(7)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public class PrimaryDoor extends Door implements TheftProof { // 初级档次门PrimaryDoor类@Overridepublic void openDoor() {// TODO Auto-generated method stubSystem.out.println("您此次打开的是初级档次门:");System.out.println("该门" + showTheftProof());}@Overridepublic void closeDoor() {// TODO Auto-generated method stubSystem.out.println("您已经关闭了该初级档次门");}@Overridepublic String showTheftProof() {return "防盗";// TODO Auto-generated method stub}}
(8)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public class MiddleDoor extends Door implements TheftProof,WaterProof, FireProof { // 中级档次门MiddleDoor类@Overridepublic String showFireProof() {// TODO Auto-generated method stubreturn "防火";}@Overridepublic String showWaterProof() {// TODO Auto-generated method stubreturn "防水";}@Overridepublic String showTheftProof() {// TODO Auto-generated method stubreturn "防盗";}@Overridepublic void openDoor() {// TODO Auto-generated method stubSystem.out.println("您此次打开的是中级档次门:");System.out.println("该门" + showTheftProof() + "," + showWaterProof() + "," + showFireProof());}@Overridepublic void closeDoor() {// TODO Auto-generated method stubSystem.out.println("您已经关闭了该中级档次门");}}
(9)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public class SeniorDoor extends Door implements TheftProof,WaterProof, FireProof,RustProof { // 高级档次门SeniorDoor类@Overridepublic String showRustProof() {// TODO Auto-generated method stubreturn "防锈";}@Overridepublic String showFireProof() {// TODO Auto-generated method stubreturn "防火";}@Overridepublic String showWaterProof() {// TODO Auto-generated method stubreturn "防水";}@Overridepublic String showTheftProof() {// TODO Auto-generated method stubreturn "防盗";}@Overridepublic void openDoor() {// TODO Auto-generated method stubSystem.out.println("您此次打开的是高级档次门:");System.out.println("该门" + showTheftProof() + "," + showWaterProof() + "," + showFireProof() + "," + showRustProof());}@Overridepublic void closeDoor() {// TODO Auto-generated method stubSystem.out.println("您已经关闭了该高级档次门");}}
(10)
package Five;
/*** @author Kun Sun* @date 2013.10.19*/
public class SpecialDoor extends Door implements TheftProof,WaterProof, FireProof,RustProof,BulletProof { // 特级档次门SpecialDoor类@Overridepublic String showBulletProof() {// TODO Auto-generated method stubreturn "防弹";}@Overridepublic String showRustProof() {// TODO Auto-generated method stubreturn "防锈";}@Overridepublic String showFireProof() {// TODO Auto-generated method stubreturn "防火";}@Overridepublic String showWaterProof() {// TODO Auto-generated method stubreturn "防水";}@Overridepublic String showTheftProof() {// TODO Auto-generated method stubreturn "防盗";}@Overridepublic void openDoor() {// TODO Auto-generated method stubSystem.out.println("您此次打开的是特级档次门:");System.out.println("该门" + showTheftProof() + "," + showWaterProof() + "," + showFireProof() + "," + showRustProof() + "," + showBulletProof()); }@Overridepublic void closeDoor() {// TODO Auto-generated method stubSystem.out.println("您已经关闭了该高级档次门");}}
(11)
package Five;public class MainClass { // 用于测试四种档次门/*** @author Kun Sun* @date 2013.10.19*/public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("欢迎来到本门展中心,欢迎参观");// 测试1,初级档次门System.out.println("\n-----------1---------------");PrimaryDoor primary = new PrimaryDoor();Door door = primary; // 使用上转型对象方式1door.openDoor();door.closeDoor();// 测试2,中级档次门System.out.println("\n-----------2---------------");door = new MiddleDoor(); // 使用上转型对象方式2door.openDoor();door.closeDoor();// 测试3,高级档次门System.out.println("\n-----------3---------------");try{ // 使用Class实例化一个对象Class cs = Class.forName("Five.SeniorDoor");door = (Door)cs.newInstance();door.openDoor();door.closeDoor();}catch(Exception e){System.out.println("不能加载类" + e.getMessage());}// 测试4,特级档次门System.out.println("\n-----------4---------------");try{ // 使用Class实例化一个对象Class cs = Class.forName("Five.SpecialDoor"); // 如果Five.SpecialDoor写成SpecialDoor会出错door = (Door)cs.newInstance();door.openDoor();door.closeDoor();}catch(Exception e){System.out.println("不能加载类" + e.getMessage());} }}
5.测试结果截图
6.备注
使用Class类创建一个实例时,若与Class相关联的类不是在默认包下,需要显示说明声明了的包,本例中,因为不是默认包,声明的包(Five)在调用时必须显示说明,如果把Class cs = Class.forName("Five.SpecialDoor");中的Five不写出,会出错,直接跳入与该try相关的catch语句中。这是因为当一个类被加载且创建对象时,和该类相关的一个类型为Class的对象就会自动创建。把相关内容自动写进与之同名的.class文件中。若不是在默认包下使用Class类创建一个实例,需显示指出,JVM才能查找到相关信息。
java新学,如果以上有什么不对的地方,恳请指出。
这篇关于用java实现抽象类门Door,能进行开门(openDoor)关门(closeDoor)操作,有防盗,防水,防火,防锈,防弹功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!