2013-10-14 54 views
0

我剛開始的Java,並通過在線的例子會在: http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.htmlJava爲什麼需要實現一個接口?

然後我得到了實現一個接口,而據我瞭解它背後的概念,但它似乎怪我,因爲在界面聲明你只定義它,並且在實現這個接口的類中,你仍然需要編寫它的函數。那爲什麼要用它呢?

我試過了示例代碼,然後更改代碼以刪除接口,並且它們都工作相同。所以我的問題是什麼時候將使用實現接口?對我來說看起來沒有必要。提前致謝!

示例代碼在線:那我改成

public class RectanglePlus 
    implements Relatable { 
    public int width = 0; 
public int height = 0; 
public Point origin; 

// four constructors 
public RectanglePlus() { 
    origin = new Point(0, 0); 
} 
public RectanglePlus(Point p) { 
    origin = p; 
} 
public RectanglePlus(int w, int h) { 
    origin = new Point(0, 0); 
    width = w; 
    height = h; 
} 
public RectanglePlus(Point p, int w, int h) { 
    origin = p; 
    width = w; 
    height = h; 
} 

// a method for moving the rectangle 
public void move(int x, int y) { 
    origin.x = x; 
    origin.y = y; 
} 

// a method for computing 
// the area of the rectangle 
public int getArea() { 
    return width * height; 
} 

// a method required to implement 
// the Relatable interface 
public int isLargerThan(Relatable other) { 
    RectanglePlus otherRect 
     = (RectanglePlus)other; 
    if (this.getArea() < otherRect.getArea()) 
     return -1; 
    else if (this.getArea() > otherRect.getArea()) 
     return 1; 
    else 
     return 0;    
} 
} 

代碼,取出接口,它仍然以同樣的

public class RectanglePlus { 
public int width = 0; 
public int height = 0; 
public Point origin; 

// four constructors 
public RectanglePlus() { 
    origin = new Point(0, 0); 
} 
public RectanglePlus(Point p) { 
    origin = p; 
} 
public RectanglePlus(int w, int h) { 
    origin = new Point(0, 0); 
    width = w; 
    height = h; 
} 
public RectanglePlus(Point p, int w, int h) { 
    origin = p; 
    width = w; 
    height = h; 
} 

// a method for moving the rectangle 
public void move(int x, int y) { 
    origin.x = x; 
    origin.y = y; 
} 

// a method for computing 
// the area of the rectangle 
public int getArea() { 
    return width * height; 
} 

// a method required to implement 
// the Relatable interface 
public int isLargerThan(RectanglePlus otherRect) { 

    if (this.getArea() < otherRect.getArea()) 
     return -1; 
    else if (this.getArea() > otherRect.getArea()) 
     return 1; 
    else 
     return 0;    
} 

public static void main(String[] args) 
{ 

    RectanglePlus newRect = new RectanglePlus(20, 30); 
    RectanglePlus somerect = new RectanglePlus(50, 100); 

    System.out.println("Area of newRect is " + newRect.getArea()); 

    System.out.println("Area of somerect is " + somerect.getArea()); 

    if((newRect.isLargerThan(somerect))==1) 
    { 
     System.out.println("newRect is bigger"); 
    } 
    else 
    { 
     System.out.println("somerect is bigger"); 
    } 

} 

} 
+2

但修改後的''那裏是Relatable'預計RectanglePlus'將無法使用...... – MadProgrammer

+0

我覺得本教程不是很清楚因爲isLargerThan只能在同一類比較對象。我做了另一個名爲SquarePlus的類,然後嘗試使用if(newRect.isLargerThan(somesquare)== 1)比較兩者。它不會工作 –

回答

2

兩個原因:

  1. 如果你有一個以上的接口的實現。假設你有Shape,子類型是RectangleOval等。如果你想編寫可以做一些形狀的代碼,你需要一個所有子類型實現的接口 - 接口是你知道的任何方法Shape會有。

  2. 如果您正在編寫一個API--您正在編寫其他人將使用的庫。你爲其他人提供接口 - 這是他們打電話可以接受的東西。你將實現接口,實現類可能有更多的方法 - 你希望能夠稍後改變這些方法,但你的用戶應該能夠拿起你的庫的新版本,並用它們的舊代碼。通過將界面與實施分離,您可以向公衆提供他們可以使用的內容,但是可以保留自己可以更改的內容而不會影響現有用戶。

+0

太好了,謝謝!但是,當我嘗試定義一個名爲SquarePlus的新類並嘗試使用newRect.isLargerThan(somesquare)比較這兩個類時,它不起作用,它說SquarePlus不能轉換爲RectanglePlus。我想我需要閱讀更多關於如何使用它的信息 –

+0

有機會舉個例子嗎?如果我想比較Square和Rectangle類,如何使用isLargerThan的接口。謝謝。 –

+0

當然,只需在界面中包含''newRectIsLargerThan()''。 –

0

這是爲了方便型/接口的重用。您可以傳遞預期有父類型對象的子類型的對象。您可以參考http://www.oodesign.com/liskov-s-substitution-principle.html

這基本上允許你以抽象的方式處理。只要他們實現某種行爲(或實現一個接口或從一個類擴展),程序就可以處理不同類的對象

+0

該鏈接示例是爲了擴展 –

0

如果您實現Relatable,它允許您在一對對象中查找最大的對象,從實現可關聯的類實例化的對象。否則,您只能在同一個類中實例化的一對對象中找到最大的對象。

相關問題