Thinking in Java之Interfaces

     首先声明一下,虽然我目前的博客中用到的英文句子和例子都是Thinking in java中的,但是这些点是我自己总结出来的需要关注的知识点,并非直接抄书的,也并非完全为翻译文章,所以文类类型选择了原创,此声明也尊重了Thinking in Java的原创,还希望大家勿喷。


Interfaces and abstract classes provide more structured way to separate interface from implementation.

    接口和抽象类是两种能够把实现和声明分离的方式。

Abstract class:

   1、If a class contains one or more abstract methods,the class itself must be qulified as abstract(otherwise,the Compiler gives you an error message.)

         如果一个类包含一个或者多个抽象方法,这个类必须被声明为抽象类,否则,编译器就会报错。

   2、抽象类不能被实例化。

   3、If you inherit from an abstract class and you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don’t (and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstractkeyword. 

          假设你创建了一个类A,A继承了一个抽象类B,你必须在A中提供B中所有抽象方法的定义,如果你不提供,编译器会强制让你把A也声明为抽象类。

   4、It’s possible to make a class abstract without including any abstractmethods. This is useful when you’ve got a class in which it doesn’t make sense to have any abstractmethods, and yet you want to prevent any instances of that class. 

         你可以创建一个没有任何抽象方法的抽象类,当你想让一个类不能被实例化的时候,这个方法是非常有用的。

Interfaces:

    1、Interface中的方法自动是public的(如果你没有给方法定义修饰符,方法就是public的)

    2、一个类可以实现多个接口(public class A implements B,C,D),这样A必须实现BCD中声明的所有方法。

    3、Keep in mind that one of the core reasons for interfaces is: to upcast to more than one base type (and the flexibility that this provides). However, a second reason for using interfaces is the same as using an abstractbase class: to prevent the client programmer from making an object of thisclass and to establish that it is only an interface.
             记住使用Interface的最主要的原因是可以把一个类向上转型为多种基类型,另一个原因和用抽象类一样,可以阻止客户端程序创建类的对象。

   4、 Should you use an interface or an abstractclass? If it’s possible to create your base class without any method definitions or member variables, you should always prefer interfaces to abstractclasses. In fact, if you know something is going to be a base class, you can consider making it an interface.

       应该使用抽象类还是接口?如果基类不需要有方法定义和成员变量的话,优先考虑使用接口。

   5、接口可以继承接口.

    

interface Monster { 
void menace(); 
} 
interface DangerousMonster extends Monster { 
void destroy(); 
} 
interface Lethal { 
void kill(); 
} 
class DragonZilla implements DangerousMonster { 
public void menace() {} 
public void destroy() {} 
} 
interface Vampire extends DangerousMonster, Lethal { 
void drinkBlood(); 
} 
class VeryBadVampire implements Vampire { 
public void menace() {} 
public void destroy() {} 
public void kill() {} 
public void drinkBlood() {} 
} 

     一般继承一个类只能用extends关键字继承一个,但是接口继承可以用extends 接口,接口2,....的形式来继承多个接口,继承之后这个接口就拥有了所有base interfaces的方法。

    6、使用接口时的命名冲突。

//: interfaces/InterfaceCollision.java 
package interfaces; 
interface I1 { void f(); } 
interface I2 { int f(int i); } 
interface I3 { int f(); } 
class C { public int f() { return 1; } } 
class C2 implements I1, I2 { 
public void f() {} 
public int f(int i) { return 1; } // overloaded 
} 
class C3 extends C implements I2 { 
public int f(int i) { return 1; } // overloaded 
} 
class C4 extends C implements I3 { 
// Identical, no problem: 
public int f() { return 1; } 
} 
// Methods differ only by return type: 
//! class C5 extends C implements I1 {} 
//! interface I4 extends I1, I3 {} ///:~ 

    重载方法不能被只根据返回值类型区分,因此最后两行编译器会报错。

    7、接口中可以有属性,但是默认是static和final的。可以使用接口名.属性名来得到接口中属性的值。

    8、接口可以在类的内部定义。如下例子:

     

//: interfaces/nesting/NestingInterfaces.java 
package interfaces.nesting; 
class A { 
interface B { 
void f(); 
} 
public class BImp implements B { 
public void f() {} 
} 
private class BImp2 implements B { 
public void f() {} 
} 
public interface C { 
void f(); 
} 
class CImp implements C { 
public void f() {} 
} 
private class CImp2 implements C { 
public void f() {} 
} 
private interface D { 
void f(); 
} 
private class DImp implements D { 
public void f() {} 
} 
public class DImp2 implements D { 
public void f() {} 
} 
public D getD() { return new DImp2(); } 
private D dRef; 
public void receiveD(D d) { 
dRef = d; 
dRef.f(); 
} 
}

      接口的用处非常非常多,由于其能够把定义和实现分离的特性,大部分设计模式中都会用到接口,来使程序的可扩展性更好。初学者可能体会不到这点,但是当接触到设计模式的时候,就会觉得接口真是个好东西。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章