1. 接口
          在某些OOP語言中使用關(guān)鍵字protocol完成類似的功能。
          字段隱式為final和static,方法隱式為public
          2. 多重繼承
          一個非抽象類必須實現(xiàn)接口的所有方法,但實現(xiàn)的方法可以從父類中繼承下來。
          如interface1定義了兩個方法f1()和f2(),father類定義了方法f1(),child類繼承father類并實現(xiàn)接口interface1,它僅需要實現(xiàn)方法f2()即可。
          3. 接口和抽象類的選擇
          如果知道某個事物應(yīng)該成為一個基類,那個第一選擇應(yīng)該是使他成為一個接口。
          4. 接口組合時的名字沖突
          在打算組合的不同接口中使用相同的方法名常常會造成代碼可讀性的混亂,要盡量避免這種情況。
          5. 群組常量
          接口可以放便地用來創(chuàng)建常量組,就像C++中的enum枚舉類型。
          public interface Months {
          int
          JANURARY = 1; FEBRUARY = 2; //...
          }
          但這樣不具備類型安全,如果需要類型安全,就需要構(gòu)建一個final類
          public final class Month {
          private static Test monitor = new Test();
          private String name;
          private Month(String nm) { name = nm; }
          public String toString() { return name; }
          public static final Month
          JAN = new Month("January"),
          FEB = new Month("February"); //...
          public static final Month[] month = {
          JAN, FEB //...
          };
          public static final Month number(int ord) {
          return month[ord - 1];
          }
          }
          通過Month.number(int)獲得Month類,由于每個月份的對象都僅存在一個,所以可以通過==或equals()判斷是否相等。
          枚舉類也可以通過org.apache.commons.lang.enum包實現(xiàn)
          6. 嵌套接口
          內(nèi)部接口可以被聲明為private,而且可以被public類實現(xiàn)
          As a new twist, interfaces can also be private, as seen in A.D (the same qualification syntax is used for nested interfaces as for nested classes). What good is a private nested interface? You might guess that it can only be implemented as a private inner class as in DImp, but A.DImp2 shows that it can also be implemented as a public class. However, A.DImp2 can only be used as itself. You are not allowed to mention the fact that it implements the private interface, so implementing a private interface is a way to force the definition of the methods in that interface without adding any type information (that is, without allowing any upcasting).
          這段不是很理解。。。
          另外,當實現(xiàn)某個接口時,并不需要實現(xiàn)嵌套在內(nèi)部的任何接口。
          7. 內(nèi)部類與向上轉(zhuǎn)型
          內(nèi)部類的真正好處在于,由于內(nèi)部類--某個接口的實現(xiàn)--可以對其他人完全隱藏,他人所能得到的僅僅是一個基類或者接口的引用,而通過那個引用甚至不能找出該引用的最終類型,這樣具體地實現(xiàn)就被完全隱藏了。
          8. 在方法和作用域中的內(nèi)部類
          這么做的兩個理由:
          1)你實現(xiàn)了某類型的接口,于是可以創(chuàng)建并返回對其的引用。
          2)你要解決一個復(fù)雜的問題,需要創(chuàng)建一個類來輔助你的解決方案,但有不希望這個類公共可用。
          這種內(nèi)部類只能在相應(yīng)作用域內(nèi)使用,除此以外,在編譯等方面和普通的類一樣。
          9. 匿名內(nèi)部類
          很有用的一個方法,在用Netbeans開發(fā)界面的時候就發(fā)現(xiàn)它的事件響應(yīng)代碼就是用這個生成的。
          startItem.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
          startItemActionPerformed(evt);
          }
          });
          注意最后的分號。
          如果匿名內(nèi)部類要使用外部的對象,參數(shù)必須設(shè)置為final,否則就會產(chǎn)生編譯錯誤。
          那么如何產(chǎn)生類似構(gòu)造器的行為呢?由于匿名,因此不可能存在構(gòu)造器,但通過instance initialization,可以達到構(gòu)造的效果。
          public class AnonymousConstructor {
          private static Test monitor = new Test();
          public static Base getBase(int i) {
          return new Base(i) {
          {
          System.out.println("Inside instance initializer");
          }

          public void f() {
          System.out.println("In anonymous f()");
          }
          };
          }
          }
          這里i不需要聲明為final,因為在內(nèi)部類中并沒有被直接使用。
          當然由于不支持重載,這類構(gòu)造方法只能有一個

          posts - 403, comments - 310, trackbacks - 0, articles - 7
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          2.20 - Java notes - Interfaces & Inner Classes

          Posted on 2007-04-22 20:24 ZelluX 閱讀(233) 評論(0)  編輯  收藏 所屬分類: OOP
          2007-02-20 13:52:16
          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 株洲市| 镶黄旗| 南木林县| 海宁市| 台东县| 防城港市| 邢台市| 比如县| 桃园市| 伊春市| 德安县| 襄城县| 永靖县| 九龙坡区| 砚山县| 亚东县| 子洲县| 泸定县| 宁南县| 星子县| 武乡县| 沧州市| 华阴市| 浠水县| 嫩江县| 克东县| 澄城县| 内乡县| 金塔县| 大连市| 深水埗区| 巴东县| 顺平县| 饶河县| 株洲县| 监利县| 邯郸县| 奉新县| 长沙市| 五莲县| 新密市|