抽象类与接口的一个程序实现

来源:互联网 发布:前端工程师 程序员 编辑:程序博客网 时间:2024/05/16 14:04
/* * 本程序实现抽象类与接口的基本操作 * 对比接口与抽象类操作的区别 * 其中的一个设计模式:工厂设计模式:A-C 直接。这样会造成耦合性过强。工厂设计模式加一个桥梁由A-B-C  * 实现间接访问 */abstract class Door{private String band;private int hight;private int length;public void setBand(String band){this.band = band;}public String getBand(){return this.band;    }public void setHight(int hight){this. hight = hight;}public float getHight(){return this.hight;}public void setLength(int length){this.length = length;}public float getLength(){return this.length;}//定义一个抽象的方法public abstract void fun(String B,int h,int L );//这里只是声明了一个在抽象类中的一个方法{}是不能出现的否则就变成了一个实现函数了}   //定义一个接口interface Access{public void info();//定义接口的方法} //定义一个测试类继承Door 完成接口Access  class test extends Door implements Access{//逻辑必须是继承写在实现方法的前面public  void fun(String B,int h,int L){//实现抽象类中的方法,作用是传递参数super.setBand(B);super.setHight(h);super.setLength(L);  }  //实现接口的方法,输出信息    public void info(){System.out.println("门的牌子是:"+getBand()+"\n"+"门的高度是:"+getHight()+"\n"+"门的宽度是 :"+getLength());//实现接口的方法}  }public class Interface{public static void main(String args[]){test I = new test();I.fun("盼盼安全门",1,2);//调用抽象类的方法I.info();//调用接口的方法   }}

原创粉丝点击