嵌套類和匿名類

原文:http://blog.163.com/c_123xyz/blog/static/7709247620087323051182/

 

 

內部類的出現
當進行Java開發時,有時需要實現一個僅包含1-2個方法的接口.在AWT和Swing開發中經常出現這種情況,例如當一個display組件需要一個 事件回調方法如一個按鈕的ActionListener時. 如果使用普通的類來實現此操作,最終會得到很多僅在單個位置上使用的小型類. 
內部類用於處理這種情況,java允許定義內部類,而且可在Gui外使用內部類.

內部類的定義和實現

內部類是指在另一個類內部定義的一個類.可以將內部類定義爲一個類的成員.
public class Linker{
  public class LinkedNode{
    private LinkedNode prev;
    private LinkedNode next;
    private String content;
    
    public LinkedNode(String content){
      this.content=content;
    }
  }
  
  public Linker(){
    LinkedNode first=new LinkedNode("First");
    LinkedNode second=new LinkedNode("Second");
    
    first.next=second;
    second.prev=first;
  }
}

定義在一個類方法中的內部類

public class Hapiness{
  interface Smiler{
    public void smile();
  }
  
  public static void main(String[] args){
    class Happy implements Smiler{
      public void smile(){
        System.out.println(":-}");
      }
    }
    
    Happy happy=new Happy();
    happy.smile();
  }
}

匿名類

對很多情況而言,定義在方法內部的類名意義不大,它可以保持爲匿名的,程序員關心的只是它的實例名.
如:
Runnable runner=new Runnable(){
     public void  run(){
          // Run statememnt
     }
}

理解匿名類

匿名類並不難理解,它只是把類的定義過程和實例的創建過程混合而已,上頁的語句實際上相當於如下語句:
// 定義類
Public class Runner implements Runnable{
     public void run(){
         // do sth
      }
}

// 創建實例
Runner runner=new Runner();

使用匿名類的篩選解耦過程

需求:從公司的職員列表中,找出男性且年齡大於22的成員.

傳統寫法:

   List allmembers=company.getMembers();// 取得所有成員
    List results=new ArrayList();// 結果列表
    
    for(Iterator it=allmembers.iterator();it.hasNext();){
     Member member=(Member)it.next();
     
     if(member.getAge()>22 && member.isMale()){  // 篩選,這裏是把查詢條件和遴選過程融合在一起,條件一變立即就得加個分支.
      results.add(member);
     }
   }

傳統方法的缺陷

這種寫法沒有錯,但是不是面向對象的寫法,它有以下缺陷:
1.查詢條件和篩選過程沒有分離.
2.這樣寫的後果使Company變成了一個失血模型而不是領域模型.
3.換查詢條件的話,上面除了"篩選"一句有變化外其它都是模板代碼,重複性很高.

使用匿名類實現的OO化查詢

真正符合OO的查詢應該是這樣:

   MemberFilter filter1=new MemberFilter(){
    public boolean accept(Member member) {
         return member.isMale() && member.getAge()>22;
    }
   };
   
   List ls=company.listMembers(filter1);
 

這段代碼成功的把查詢條件作爲一個接口分離了出去,接口代碼如下:

public interface MemberFilter{
  public boolean accept(Member member); 
}

查詢函數的變化

而類Company增加了這樣一個函數:

public List searchMembers(MemberFilter memberFilter){
   List retval=new ArrayList();
    
    for(Iterator it=members.iterator();it.hasNext();){
     Member member=(Member)it.next();
     
     if(memberFilter.accept(member)){
      retval.add(member);
    }
   }  
   
   return retval;
}
這就把模板代碼歸結到了類內部,外面不會重複書寫了.Company也同時擁有了數據和行爲,而不是原來的數據容器了.


匿名類的例子二

用匿名類處理分類彙總的方法 分類彙總是統計中常用,舉例來說如統計學生成績,及格不及格的歸類,分優良中差等級歸類等,每個單項代碼很好寫,但是如果分類彙總的項目多了,能一種彙總 寫一個函數嗎? 比如說有些科目60分纔算及格,有些科目50分就算;有些老師喜歡分優良中差四等,有些老師卻喜歡分ABCD;不一而足,如果每個都寫一個函數無疑是個編 寫和維護惡夢. 如果我們用匿名類把分類彙總的規則和分類彙總的過程分別抽象出來,代碼就清晰靈活多了,以下代碼講述了這個過程.

基本類Student

public class Student{
    private String name;
    private int score;
    
    public Student(String name,int score){
        this.name=name;
        this.score=score;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getScore() {
        return score;
    }
    public void setScore(int score) {
        this.score = score;
    }    
}


用於分類彙總的類

它強制子類實現getKey和getvalue兩個方法:
public abstract class ClassifyRule {
    public Student student;
    
    public ClassifyRule(){        
    }   

    public void setStudent(Student student) {
        this.student = student;
    }
    
    abstract public String getKey();
    abstract public int getValue();
}

對Student進行統計處理的StudentService類

注意getSum方法,它保留了篩選過程,篩選規則則不在其中:
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class StudentService {
    private List<Student> students;

    public StudentService() {
        students = new ArrayList<Student>();
    }

    public void add(Student student) {
        students.add(student);
    }

    public Hashtable<String, Integer> getSum(ClassifyRule rule) {
        Hashtable<String, Integer> ht = new Hashtable<String, Integer>();

        for (Student student : students) {
            rule.setStudent(student);
            String key = rule.getKey();
            int value = rule.getValue();

            if (ht.containsKey(key)) {
                Integer oldValue = ht.remove(key);
                oldValue += value;
                ht.put(key, oldValue);
            } else {
                ht.put(key, value);
            }
        }

        return ht;
    }
}

測試代碼,注意其中篩選規則的創建

public class Test {
    public static void main(String[] args) {
        // 初始化
        StudentService service = new StudentService();
        service.add(new Student("Andy", 90));
        service.add(new Student("Bill", 95));
        service.add(new Student("Cindy", 70));
        service.add(new Student("Dural", 85));
        service.add(new Student("Edin", 60));
        service.add(new Student("Felix", 55));
        service.add(new Student("Green", 15));

        // 60分及格篩選
        ClassifyRule rule60 = new ClassifyRule() {
            public String getKey() {
                return student.getScore() >= 60 ? "及格" : "不及格";
            }

            public int getValue() {
                return 1;
            }
        };

        System.out.println("60分及格篩選");
        printHt(service.getSum(rule60));

        // 50分及格篩選
        ClassifyRule rule50 = new ClassifyRule() {
            public String getKey() {
                return student.getScore() >= 50 ? "及格" : "不及格";
            }

            public int getValue() {
                return 1;
            }
        };
        System.out.println("\n50分及格篩選");
        printHt(service.getSum(rule50));

        // 分"優良中差"等級
        ClassifyRule ruleCn = new ClassifyRule() {
            public String getKey() {
                String retval = "";

                int score = student.getScore();
                if (score >= 90) {
                    retval = "優";
                } else if (score >= 80) {
                    retval = "良";
                } else if (score >= 60) {
                    retval = "中";
                } else if (score > 0) {
                    retval = "差";
                }

                return retval;
            }

            public int getValue() {
                return 1;
            }
        };

測試代碼

System.out.println("\n分優良中差等級篩選");
        printHt(service.getSum(ruleCn));

        // 分"ABCD"等級
        ClassifyRule ruleWest = new ClassifyRule() {
            public String getKey() {
                String retval = "";

                int score = student.getScore();
                if (score >= 90) {
                    retval = "A";
                } else if (score >= 80) {
                    retval = "B";
                } else if (score >= 60) {
                    retval = "C";
                } else if (score > 0) {
                    retval = "D";
                }

                return retval;
            }

            public int getValue() {
                return 1;
            }
        };

        System.out.println("\n分ABCD等級篩選");
        printHt(service.getSum(ruleWest));
    }

    private static void printHt(Hashtable ht) {
        for (Iterator it = ht.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            Integer value = (Integer) ht.get(key);
            System.out.println("Key=" + key + " Value=" + value);
        }
    }
}


測試結果如下:

 

60分及格篩選
Key=及格 Value=5
Key=不及格 Value=2

50分及格篩選
Key=及格 Value=6
Key=不及格 Value=1

分優良中差等級篩選
Key=優 Value=2
Key=良 Value=1
Key=中 Value=2
Key=差 Value=2

分ABCD等級篩選
Key=A Value=2
Key=D Value=2
Key=C Value=2
Key=B Value=1

後記

內部類也叫嵌套類,一般不提倡書寫,但它在java核心類中都存在,如接口Map中的Entry,我們應該瞭解並能解讀這種方法.

匿名類相對而言有用得多,在解耦合和事件回調註冊中很常見,大家應該對它的運用融會貫通.
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章