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(); 
} 
}

      接口的用處非常非常多,由於其能夠把定義和實現分離的特性,大部分設計模式中都會用到接口,來使程序的可擴展性更好。初學者可能體會不到這點,但是當接觸到設計模式的時候,就會覺得接口真是個好東西。

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