2013-10-14 52 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,它允许您在一对对象中查找最大的对象,从实现可关联的类实例化的对象。否则,您只能在同一个类中实例化的一对对象中找到最大的对象。

相关问题