1. 接口
          在某些OOP語言中使用關(guān)鍵字protocol完成類似的功能。
          字段隱式為final和static,方法隱式為public
          2. 多重繼承
          一個(gè)非抽象類必須實(shí)現(xiàn)接口的所有方法,但實(shí)現(xiàn)的方法可以從父類中繼承下來。
          如interface1定義了兩個(gè)方法f1()和f2(),father類定義了方法f1(),child類繼承father類并實(shí)現(xiàn)接口interface1,它僅需要實(shí)現(xiàn)方法f2()即可。
          3. 接口和抽象類的選擇
          如果知道某個(gè)事物應(yīng)該成為一個(gè)基類,那個(gè)第一選擇應(yīng)該是使他成為一個(gè)接口。
          4. 接口組合時(shí)的名字沖突
          在打算組合的不同接口中使用相同的方法名常常會(huì)造成代碼可讀性的混亂,要盡量避免這種情況。
          5. 群組常量
          接口可以放便地用來創(chuàng)建常量組,就像C++中的enum枚舉類型。
          public interface Months {
          int
          JANURARY = 1; FEBRUARY = 2; //...
          }
          但這樣不具備類型安全,如果需要類型安全,就需要構(gòu)建一個(gè)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類,由于每個(gè)月份的對(duì)象都僅存在一個(gè),所以可以通過==或equals()判斷是否相等。
          枚舉類也可以通過org.apache.commons.lang.enum包實(shí)現(xiàn)
          6. 嵌套接口
          內(nèi)部接口可以被聲明為private,而且可以被public類實(shí)現(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).
          這段不是很理解。。。
          另外,當(dāng)實(shí)現(xiàn)某個(gè)接口時(shí),并不需要實(shí)現(xiàn)嵌套在內(nèi)部的任何接口。
          7. 內(nèi)部類與向上轉(zhuǎn)型
          內(nèi)部類的真正好處在于,由于內(nèi)部類--某個(gè)接口的實(shí)現(xiàn)--可以對(duì)其他人完全隱藏,他人所能得到的僅僅是一個(gè)基類或者接口的引用,而通過那個(gè)引用甚至不能找出該引用的最終類型,這樣具體地實(shí)現(xiàn)就被完全隱藏了。
          8. 在方法和作用域中的內(nèi)部類
          這么做的兩個(gè)理由:
          1)你實(shí)現(xiàn)了某類型的接口,于是可以創(chuàng)建并返回對(duì)其的引用。
          2)你要解決一個(gè)復(fù)雜的問題,需要?jiǎng)?chuàng)建一個(gè)類來輔助你的解決方案,但有不希望這個(gè)類公共可用。
          這種內(nèi)部類只能在相應(yīng)作用域內(nèi)使用,除此以外,在編譯等方面和普通的類一樣。
          9. 匿名內(nèi)部類
          很有用的一個(gè)方法,在用Netbeans開發(fā)界面的時(shí)候就發(fā)現(xiàn)它的事件響應(yīng)代碼就是用這個(gè)生成的。
          startItem.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
          startItemActionPerformed(evt);
          }
          });
          注意最后的分號(hào)。
          如果匿名內(nèi)部類要使用外部的對(duì)象,參數(shù)必須設(shè)置為final,否則就會(huì)產(chǎn)生編譯錯(cuò)誤。
          那么如何產(chǎn)生類似構(gòu)造器的行為呢?由于匿名,因此不可能存在構(gòu)造器,但通過instance initialization,可以達(dá)到構(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,因?yàn)樵趦?nèi)部類中并沒有被直接使用。
          當(dāng)然由于不支持重載,這類構(gòu)造方法只能有一個(gè)

          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 閱讀(231) 評(píng)論(0)  編輯  收藏 所屬分類: OOP
          2007-02-20 13:52:16
          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 遂平县| 曲周县| 堆龙德庆县| 孝义市| 荣昌县| 绥棱县| 上饶县| 连江县| 深圳市| 澄迈县| 崇信县| 喀什市| 乌鲁木齐市| 寿阳县| 新疆| 通州市| 东乌珠穆沁旗| 公安县| 鄂尔多斯市| 平阳县| 石河子市| 汨罗市| 门源| 安徽省| 陕西省| 宁武县| 商丘市| 武威市| 鄂托克前旗| 梅州市| 通渭县| 平乡县| 枝江市| 淮滨县| 平果县| 娄底市| 汕头市| 安庆市| 紫阳县| 新宾| 宁强县|