3.5 Interface interface
① sometimes must be derived from several classes in a subclass inherits all of their properties and methods. However, Java does not support multiple inheritance. With the interface, you can get the effect of multiple inheritance.
② Interface (interface) is defined by a set of constant values and abstract methods.
③ In essence, the interface is a special abstract class, which defines only the abstract class contains constants and methods, without achieved variables and methods.
3.5.1 Interface basic grammar:
Interface is also a reference type, the class can be seen as equivalent.
① how to define interfaces, syntax:
[Modifier] interface interface name} {
Constant, abstract methods: ② interface can appear.
All member variables in the interface are by default public static final modification.
All interface methods are by default public abstract modified.
③ In fact, the interface is a special abstract class, a special interface is completely abstract.
④ interface constructor does not and can not be instantiated.
⑤ can be multiple inheritance between interfaces and interfaces.
⑥ a class can implement multiple interfaces. (Here the "realization" can be seen as equivalent to "inherit")
⑦ a non-abstract class implements an interface, the interface will need all methods "implemented / rewrite / coverage", before instantiation. Otherwise, the class is still abstract.
⑧ class implements the interface if not all interface methods implemented must be defined as an abstract class such
⑨ main purpose is to be the interface implementation class implementation. (Oriented Programming Interface)
Similar inheritance, polymorphism between the interface and implementation class.
The syntax definition of Java classes: the first to write extends, after writing implements
public interface A{ // constants ( must be public static final modification ) public static final String SUCCESS = "success"; public static final double PI = 3.14; MAX_VALUE = 127 byte; // constants // public static final can be omitted. // abstract method ( all abstract interface methods are public abstract) public abstract void m1(); M2 void (); // public abstract may be omitted. } interface B{ void m1(); } interface C{ void m2(); } interface D{ void m3(); } interface E extends B,C,D{ void m4(); } // implements realize the meaning of a keyword. // implements and extends the same meaning. class MyClass implements B,C{ public void m1(){} public void m2(){} } class F implements E{ public void m1(){} public void m2(){} public void m3(){} public void m4(){} } |
3.5.2 interface role
① can make the project a layered, all the layers are oriented interface development, improve the development efficiency.
② interface enables the degree of coupling between codes, and code decreases, as the relationship between the motherboard and memory, to be "removable." You can switch freely.
③ interfaces and abstract classes able to complete a certain function, priority selection interface . Because the interface can be more achieved multiple inheritance. And in addition to a class that implements the interface, you can also go to inherit from other classes (to retain the class inheritance).
举例1:
public interface CustomerService{
//定义一个退出系统的方法 void logout();
} |
/* 编写接口的实现类. 该类的编写完全是面向接口去实现的。 */ public class CustomerServiceImpl implements CustomerService{ //对接口中的抽象方法进行实现。 public void logout(){ System.out.println("成功退出系统!"); } } |
public class Test{ //入口 public static void main(String[] args){ //要执行CustomerServiceImpl中的logout方法.
//以下程序面向接口去调用 CustomerService cs = new CustomerServiceImpl(); //多态 //调用 cs.logout(); } } |
举例2:
/* 汽车和发动机之间的接口。
生产汽车的厂家面向接口生产。 生产发动机的厂家面向接口生产。 */ public interface Engine{
//所有的发动机都可以启动. void start(); } |
//生产汽车 public class Car{
//Field //引擎 //面向接口编程 Engine e;
//Constructor Car(Engine e){ this.e = e; }
//汽车应该能够测试引擎 public void testEngine(){ e.start();//面向接口调用 } } |
public class YAMAHA implements Engine{ public void start(){ System.out.println("YAMAHA启动"); } } |
public class HONDA implements Engine{ public void start(){ System.out.println("HONDA启动"); } } |
public class Test{
public static void main(String[] args){
//1.生产引擎 YAMAHA e1 = new YAMAHA();
//2.生产汽车 Car c = new Car(e1);
//3.测试引擎 c.testEngine();
//换HONDA c.e = new HONDA();
c.testEngine(); } } |
3.5.3接口的应用:工厂方法(FactoryMethod)& 代理模式(Proxy)
①工厂方法(FactoryMethod)
概述:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。FactoryMethod使一个类的实例化延迟到其子类。
适用性:
1)当一个类不知道它所必须创建的对象的类的时候
2)当一个类希望由它的子类来指定它所创建的对象的时候
3)当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候
工厂方法举例:
总结:
FactoryMethod模式是设计模式中应用最为广泛的模式,在面向对象的编程中,对象的创建工作非常简单,对象的创建时机却很重要。FactoryMethod解决的就是这个问题,它通过面向对象的手法,将所要创建的具体对象的创建工作延迟到了子类,从而提供了一种扩展的策略,较好的解决了这种紧耦合的关系。
//接口的应用:工厂方法的设计模式
public class TestFactoryMethod { public static void main(String[] args) { IWorkFactory i = new StudentWorkFactory(); i.getWork().doWork();
IWorkFactory i1 = new TeacherWorkFactory(); i1.getWork().doWork(); } }
interface IWorkFactory{ Work getWork(); } |
class StudentWorkFactory implements IWorkFactory{
@Override public Work getWork() { return new StudentWork(); }
} class TeacherWorkFactory implements IWorkFactory{
@Override public Work getWork() { return new TeacherWork(); }
}
interface Work{ void doWork(); } class StudentWork implements Work{ @Override public void doWork() { System.out.println("学生写作业"); } } class TeacherWork implements Work{ @Override public void doWork() { System.out.println("老师批改作业"); } } |
②代理模式(Proxy)
概述:
为其他对象提供一种代理以控制对这个对象的访问。
interface Object{ void action(); } class ProxyObject implements Object{ Object obj; public void action(){ System.out.println("代理开始"); obj.action(); System.out.println("代理结束"); } public ProxyObject(){ System.out.println("这是代理类"); obj = new ObjectImpl(); } } class ObjectImpl implements Object{ public void action(){ System.out.println("======"); System.out.println("======"); System.out.println("被代理的类"); System.out.println("======"); System.out.println("======"); } } public class Test2 { public static void main(String[] args) { Object ob = new ProxyObject(); ob.action(); } } |
//接口的应用:代理模式(静态代理)
public class TestProxy { public static void main(String[] args) { Object obj = new ProxyObject(); obj.action(); } } interface Object{ void action(); } //代理类 class ProxyObject implements Object{ Object obj; public ProxyObject(){ System.out.println("代理类创建成功"); obj = new ObjctImpl(); } public void action(){ System.out.println("代理类开始执行"); obj.action(); System.out.println("代理类执行结束"); } } //被代理类 class ObjctImpl implements Object{ @Override public void action() { System.out.println("=====被代理类开始执行======"); System.out.println("=====具体的操作======"); System.out.println("=====被代理类执行完毕======"); } } |