`
lovnet
  • 浏览: 6702021 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

Oracle培训(九)——Core Java第九章知识点总结——接口

 
阅读更多

Core Java第九章知识点总结——接口

知识点预览


接口的概念

接口的多继承

子类的多实现

使用接口的好处


接口的概念

1. 什么是接口

接口是特殊的抽象类

属性:公开 静态 常量

抽象类

方法:公开 抽象

2.interface关键字(abstract class)

a) 接口------编译------->.class文件

b) 特殊抽象类

publicstatic final

c) 可省略

public abstract

d) 接口:可声明引用,不能创建对象抽象类可定义构造方法,但接口不可,而且不会提供默认的。

3. implements关键字

a)实现接口--------->所有方法均实现

b) MyInterface m1 = new MyClass();

c) 接口的特点

i. 接口的继承

1. 接口之间继承:extends

package chp9.ex03;
/**
 * 
 * 说明:本程序演示了接口的继承
 */
interface IA {
  void m1();
}

interface IB extends IA{
  void m2();
}

class MyClass implements IB{
	public void m1(){
	  System.out.println("this is m1()");
	}
	public void m2(){
	  System.out.println("this is m2()");	
	}
}

public class TestInterface1 {
  public static void main(String args){
	  
	  MyClass m1 = new MyClass();
	  
	  IA m2 = new MyClass();//多态的使用
	  
	  IB m3 = new MyClass();//多态的使用
  }
}


2.接口和多态密不可分


接口的多继承

package chp9.ex04;

/**
 * 
 * 说明:本程序演示了接口的多继承
 */
interface IA {
  void m1();
}

interface IB {
  void m2();
}

interface IC extends IA,IB{
  void m3();
}

class MyClass implements IC{
	public void m1(){
	  System.out.println("this is m1()");
	}
	public void m2(){
	  System.out.println("this is m2()");	
	}
	public void m3(){
	  System.out.println("this is m3()");	
	}
}

public class TestInterface1 {
  public static void main(String args){
	  
	  MyClass m1 = new MyClass();
	  
	  IA m2 = new MyClass();
	  
	  IB m3 = new MyClass();
	  
	  IC m4 = new MyClass();
	  
  }
}

子类的多实现

1. 子类的多实现

2.子类的多实现的同时还可以继承一个父类

extends写在implements之前

class MyClass extends CCimplements IA,IB{

}

package chp9.ex06;
/**
 * 
 * 说明:本程序演示了类的继承和多实现
 */
interface IA {
  void m1();
}

interface IB {
  void m2();
}

class CC{
  void m3(){};	
}

class MyClass extends CC  implements IA,IB {
	public void m1(){
	  System.out.println("this is m1()");
	}
	public void m2(){
	  System.out.println("this is m2()");	
	}
	//方法的覆盖
	public void m3(){
		System.out.println("this is m3()");
	}
}



public class TestInterface1 {
  public static void main(String[] args){
	  MyClass m1 = new MyClass();
	  System.out.println(m1 instanceof IA);
	  System.out.println(m1 instanceof IB);
	  System.out.println(m1 instanceof CC);
  }
}


使用接口的好处


1.增加程序的通用性

a) 声明引用时要使用接口类型

b) 方法的参数要什么成接口类型

c) 方法的返回值要声明成接口类型

2.接口实现了特殊的多继承

3.解耦合(最主要的作用)


通过接口把强耦合转化为弱耦合

无接口:


有接口:



相机为什么不用抽象类:这样也造成了多继承

为什么手机不继承相机:普通手机也有照相功能


package chp9.ex07;
/**
 * 
 * 说明:本程序演示了接口的好处——解耦合
 */
public class TestBuble {
    public static void main(String args[]){
    	
    	RedBuble red = new RedBuble();
    	GreenBuble green = new GreenBuble();
    	
    	Lamp l = new Lamp();
    	
    	l.setBuble(red);
    	l.on();
    	
    	l.setBuble(green);
    	l.on();
    }
}

interface Buble{
	void light();
}

class RedBuble implements Buble{
   public void light(){
	 System.out.println("red shine");	
   }
}
class GreenBuble implements Buble{
	public void light(){
	  System.out.println("green shine");
	}
}

class Lamp {
  private Buble buble;	
  
  //安装灯泡的灯口
  public void setBuble(Buble buble){
	  this.buble = buble;
  }
  //开灯
  public void on(){
	  buble.light();
  }
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics