/*
* 本程序实现抽象类与接口的基本操作
* 对比接口与抽象类操作的区别
* 其中的一个设计模式:工厂设计模式: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();//调用接口的方法
}
}抽象类与接口的一个程序实现
最新推荐文章于 2024-12-12 22:40:27 发布
本文通过具体示例展示了抽象类与接口的基本使用方法,并对比了两者之间的差异。同时介绍了工厂设计模式的应用,以及如何通过引入桥梁模式降低系统耦合度。
1523

被折叠的 条评论
为什么被折叠?



