Java为什么要实现必要的接口?

时间:2013-10-14 06:38:28

标签: java interface implements

我刚刚开始使用Java,并且正在网上查看以下示例: http://docs.oracle.com/javase/tutorial/java/IandI/usinginterface.html

然后我开始实现一个接口,虽然我理解它背后的概念,但它对我来说似乎很奇怪,因为你只定义它的接口声明,并且在实现这个接口的类中你仍然需要写它的功能。那么为什么要使用呢?

我尝试了示例代码,然后更改代码以删除界面,它们的工作方式相同。所以我的问题是何时使用实现接口?我看起来没必要。提前谢谢!

在线示例代码:

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");
    }

}

}

3 个答案:

答案 0 :(得分:2)

有两个原因:

  1. 如果您有多个接口实现。假设您有Shape,子类型为RectangleOval等。如果您想编写一般可以对形状执行某些操作的代码,则需要一个所有子类型都实现的接口 - 界面是您知道任何Shape将具有的方法集。

  2. 如果您正在编写API - 您正在编写其他人将使用的库。你为其他人提供接口 - 这是他们可以打电话的东西。您将实现该接口,并且实现类可能有更多方法 - 您希望以后能够更改这些方法,但您的用户应该能够选择新版本的库并将其与旧代码一起使用。通过将界面与实现分离,您可以向公众提供他们可以使用的内容,但可以保留您可以更改的内容,而不会伤害现有用户。

答案 1 :(得分:0)

这是为了便于类型/接口重用。您可以传递子类型的对象,其中父类型对象是预期的。您可以参考http://www.oodesign.com/liskov-s-substitution-principle.html

这基本上允许您以抽象的方式处理。只要程序实现某些行为(或实现接口或从类扩展),您的程序就可以处理不同类的对象。

答案 2 :(得分:0)

如果实现Relatable,它允许您在实现Relatable的类中实例化的任何对象中找到一对对象中的最大对象。否则,您只能找到从同一个类实例化的一对对象中的最大对象。

相关问题