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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值