2011-02-28 40 views
1

我想了解Java OOP概念的基礎知識,所以我有一個關於接口的問題,因爲它讓我有點困惑。下面我正在玩兩個班。其中一個實現了SizeComparable接口,另一個則不工作。爲什麼我應該在Java中使用這種接口?

public interface SizeComparable { 
    int isHigher(SizeComparable obj); 
} 

public class Interesting implements SizeComparable { 

    private int height; 

    public Interesting(int height) { 
     this.height = height; 
    } 

    public int getHeight() { 
     return height; 
    } 

    public int isHigher(SizeComparable obj) { 
     Interesting otherInteresting = (Interesting)obj; 
     if(this.getHeight() > otherInteresting.getHeight()) { 
      return 1; 
     } else { 
      return 0; 
     } 
    } 

    public static void main(String[] args) { 
     Interesting i1 = new Interesting(182); 
     Interesting i2 = new Interesting(69); 

     int result = i1.isHigher(i2); 

     System.out.println("Is i1 higher than i2? Result: " + result); 
    } 

} 

上面的代碼如何比下面的代碼更好?就我個人而言,我不明白,因爲代碼咆哮那些工作也很棒。我是否錯過了界面構思背後的一些概念?

public class Interesting { 

    private int height; 

    public Interesting(int height) { 
     this.height = height; 
    } 

    public int getHeight() { 
     return height; 
    } 

    public int isHigher(Interesting obj) { 
     if(this.getHeight() > obj.getHeight()) { 
      return 1; 
     } else { 
      return 0; 
     } 
    } 

    public static void main(String[] args) { 
     Interesting i1 = new Interesting(182); 
     Interesting i2 = new Interesting(69); 

     int result = i1.isHigher(i2); 

     System.out.println("Is i1 higher than i2? Result: " + result); 
    } 

} 

我試圖理解它(here),但我仍然不確定這一點。對不起,如果這個問題有點愚蠢,我只想完全理解它。

+0

一個接口允許有人從頭開始實現你的接口,或者在原始或主要目的與你的接口完全不同的其他代碼中實現你的接口。 – yogsma 2011-02-28 22:23:17

回答

7

如果你有InterestingBoringIndifferentCrazy類,所有代表的一些對象的高度相當,然後所有的人都可以實現SizeComparable接口,因此可以相互媲美。

沒有接口,你需要每個類中的n個方法來比較它與自身和所有其他方法。

+1

這四個中哪一個是你*? :)(記住,Java不允許多重繼承!) – corsiKa 2011-02-28 22:19:18

+0

但是行:*有趣的otherInteresting =(有趣的)obj; *將對象轉換爲有趣的。我如何將它轉換爲Boring,Crazy或其他對象將使用getHeight()方法的方式? – Rihards 2011-02-28 22:22:14

+4

@Richards - 你必須在界面中定義'getHeight()'。 – Bozho 2011-02-28 22:23:30

1

在開始時,它可能沒有多大意義,但是當你開始注入依賴項時,開始測試或者將寫入多於一個接口的實現,而不是真的會給你提升。

此外它允許多重繼承。有時候你想要類似可比較的東西 - 非常通用的接口,可以被系統中的很多類使用。這將伴隨着更大的系統和更大的班級層次。

Java世界的現在只相信休息,並使用這些接口:)

和好運

+0

的答案謝謝你的幫助! :) – Rihards 2011-02-28 22:26:45

1

接口是任何類希望實現接口同意遵守合同。之所以使用的界面允許一些其他類或方法來訪問接口功能,而不需要在您的類從一個共同的類繼承...我會修改你的榜樣,以使其更清晰:

public interface HeightCapable { 
    int getHeight(); 
} 

public class Interesting implements HeightCapable { 

    private int height; 

    public Interesting(int height) { 
     this.height = height; 
    } 

    public int getHeight() { 
     return height; 
    } 

} 

public class SomeOtherClass { 
    public boolean isHigher(HeightCapable obj1, HeightCapable obj2) { 
     // ... do something interesting 
     if (obj1.getHeight() > obj2.getHeight()) { 
      return true; 
     } 
} 

在上面的例子中,任何實現HeightCapable接口的類都可以調用SomeOtherClass.isHigher()。沒有接口,任何希望調用SomeOtherClass.isHigher()的類都需要從一個普通的類繼承。 Java缺乏多重繼承。

+0

這與* SomeOtherClass.isHigher()*方法混淆我比我以前更多。 Heheh。 – Rihards 2011-02-28 22:57:21

1

如果您希望您的SizeComparable對象不是與所有其他SizeComparable對象相媲美,而只是針對某些類型的對象,則可以使用泛型類型。

interface SizeComparable<X> { 

    /** 
    * returns true if this object is higher than that object. 
    */ 
    boolean isHigher(X that); 

} 

然後,你可以創建你實現這樣的:

public class Interesting implements SizeComparable<Interesting> { 

    ... 

    public boolean isHigher(Interesting obj) { 
     return this.getHeight() > obj.getHeight(); 
    } 

} 

或者,你甚至可以有另一種接口

public interface HeigthHaving extends SizeComparable<HeightHaving> { 

    /** 
    * returns the height of this object. 
    */ 
    public int getHeigth(); 


    /** 
    * compares this object's height with another objects height. 
    * @return true if this.getHeight() > that.getHeight, else false. 
    */ 
    public boolean isHigher(HeightHaving that); 

} 

現在HeightHaving的每一個實現必須實現isHigher(HeightHaving)方法(這即使我們在這裏沒有重複,情況也是如此),並且應該按照這裏的規範來做。儘管如此,其他SizeComparable實現並未受到影響。

這裏的好處是,現在例如排序算法可以對任何類型爲X的列表/數組進行排序,以實現SizeComparable,因此,您不必爲每種可能想按高度排序的新對象重新編寫它。

(事實上,已經有類似的界面Comparable<X>標準的API中。也許你要改用您SizeComparable這一點。)

順便說一句,對於isXXX方法通常是一個布爾返回類型爲比整數更合理。

+0

是的,我同意,* isSomething *應該返回布爾值,但我只是用0和1來輕鬆打印出來。但是,謝謝你,會盡力完全理解它。 – Rihards 2011-03-01 00:36:14

相關問題