接口的基本语法与应用28,29

接口的基本语法(一)

1.使用interface定义;

2.接口当中的方法都是抽象方法;

3.接口当中的方法都是public权限;


使用implements, 实现 是特殊的 继承;

interface USB{
		public void read();
		
		public void write();
	}

定义一个接口,用interface来定义

class USBPhone implements USB{
		public void read(){
				System.out.println("读");
			}
			
		public void write(){
				System.out.println("写");
			}
	}

定义一个子类或则实现类来实现这个接口,一个类实现了一个接口,会继承这个接口里面的抽象方法,然后在子类里面对抽象方法进行复写。

class Test{
		public static void main(String args[]){
				USBPhone usbPhone = new USBPhone();
				USB usb = usbPhone;
				usb.read();
				usb.write();
			}
	}


结果:

接口的基本语法(二)

1.实现接口使用implements关键字

2.一个类可是实现多个接口

3.一个接口可以继承多个接口


理解:2.手机可以通过USB连接电脑,也可以使用WIFI连接电脑,这部手机既支持USB标准,又支持WIFI标准。

            3.

interface USB{
		public void read();
		
		public void write();
		
	
	}

interface WIFI{
		public void open();
		
		public void close();
	}

class Phone implements USB,WIFI{
		public void read(){
				System.out.println("读");
			}
			
		public void write(){
				System.out.println("写");				
			}
			
		
			public void open(){
				System.out.println("打开");
				
			}	
			
		public void close(){
				System.out.println("关闭");
			}
	}

class Test{
		public static void main(String args[]){
				Phone phone = new Phone();
				
				USB usb = phone;//两种转型方式,一张转型为USB
				usb.read();
				usb.write();
				
				WIFI wifi =phone;//另一种转型为WIFI
				wifi.open();
				wifi.close();
			}
	}

两种向上转型。


interface A{
			public void funA();
		}

interface B{
			public void funB();
		}

	interface C extends A,B{
			public void funC();
		}

看C这个接口,C不是实现A,B这两个接口,而是继承,为什么不是实现(implements),要实现A,B就要去复写A,B里面的抽象方法,这样的话C就不可能是个接口,所以C是继承(extends)A,B,继承了A,B里的funA(),funB(),再加上C中的funC(),C接口就有3个方法了。当我们要用一个类,实现C的时候,必须复写这3个方法,这就是一个接口可以继承另外的多个接口的含义。

接口的应用

1.为什么要使用接口

2.工厂方法模式


interface Printer{
		public void open();
		
		public void close();
		
		public void print(String s);
		
	}

class HpPrinter implements Printer{
		public void open(){
				System.out.println("惠普开机");
			}
		
		public void close(){
				System.out.println("惠普关机");
			}
		
		public void print(String s){
				System.out.println("惠普打印"+s);
			}
	}

class CannonPrinter implements Printer{
		private void clean(){
				System.out.println("佳能清理");
			}
			
			public void open(){
					this.clean();
					System.out.println("佳能开机");
				}
		
		public void close(){
				System.out.println("佳能关机");
			}
		
		public void print(String s){
				System.out.println("佳能打印"+s);
			}
	}

class Test{
<span style="white-space:pre">		</span>public static void main(String args[]){
<span style="white-space:pre">			</span>//根据用户的选择,生成相应的打印机对象,并且向上转型为Printer
<span style="white-space:pre">				</span>Printer p = null;
<span style="white-space:pre">				</span>
<span style="white-space:pre">				</span>int flag = 1;
<span style="white-space:pre">				</span>if(flag==0){
<span style="white-space:pre">						</span>p = new HpPrinter();
<span style="white-space:pre">					</span>}
<span style="white-space:pre">				</span>else if(flag == 1){
<span style="white-space:pre">						</span>p = new CannonPrinter();
<span style="white-space:pre">					</span>}
<span style="white-space:pre">					</span>
<span style="white-space:pre">				</span>p.open();
<span style="white-space:pre">				</span>p.print("Test");
<span style="white-space:pre">				</span>p.close();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">	</span>}
结果:


现在问题来了,电脑上有100个功能需要调用打印机,Test里面的代码要写100 遍吗?

解决:把重复代码封装起来,需要的时候直接调用。定义一个类,把功能写在里面。添加一个PrinterFactory.java ,修改Test.java  。

class PrinterFactory{
		public static Printer getPrinter(int flag){
				Printer p = null;
				
				
				if(flag==0){
						p = new HpPrinter();
					}
				else if(flag == 1){
						p = new CannonPrinter();
					}
					
				return p;
			}
	
	}

class Test{
		public static void main(String args[]){
			int flag =0;
			
			//如果PrinterFactory 中 的getPrinter() 前不加static 的话,无法从静态上下文中引用非静态方法,必须写成如下注释
			/* PrinterFactory b = new PrinterFactory();
			   Printer p = b.getPrinter(flag);   
			*/
			
			//如果加了static,静态函数也可以用类名来调用 
			Printer p = PrinterFactory.getPrinter(flag);
									
				p.open();
				p.print("Test");
				p.close();
			}
	}

这样的话,如果再添加一个打印机,就不用在100的功能里面修改那么多重复代码了,只需要在PrinterFactory里面加入打印机即可。这就是著名的工厂方法模式:


工厂方法模式的思路很简单:把生成对象的代码,或者使用new 来调用构造函数的代码把它封装在工厂类当中。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值