首页
学习
活动
专区
圈层
工具
发布

Java:接口
EN

Stack Overflow用户
提问于 2014-07-17 11:35:08
回答 6查看 591关注 0票数 0

我一直在阅读关于java接口的文章。总的来说,我理解这个概念,除了一个问题。在http://goo.gl/l5r69 (docs.oracle)中,注释中写道,我们可以键入强制转换接口和实现接口的类。那是

代码语言:javascript
复制
public interface Relatable { 
    public int isLargerThan (Relatable other) ; 
} 

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

    // four constructors 
    // ...

    // 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;
        }
    } 
}

如何将otherRect (即接口)转换为RectanglePlus。混淆之处在于,RectanglePlus是一个具有变量的,这些变量不存在于otherRect (即接口)中。

EN

回答 6

Stack Overflow用户

发布于 2014-07-17 11:46:05

您的界面非常类似于可比的 (您确定可比较的不是您想要的吗?)因此,也许您应该在其中添加一个泛型:

代码语言:javascript
复制
public interface Relatable<T extends Relatable> {

    public int isLargerThan(T t);
}

然后你的课就从以下几个开始:

代码语言:javascript
复制
 public class RectanglePlus implements Relatable<RectanglePlus> {...

因此,您的RectanglePlus实例将仅与其他RectanglesPlus元素进行比较。

如果这不适合您的需要,那么您必须选择在比较两个不同的类时会发生什么:

代码语言:javascript
复制
public class RectanglePlus implements Relatable {
    public int width = 0;
    public int height = 0;
    public Point origin;

    public int getArea() {
        return width * height;
    }

    public int isLargerThan(Relatable other) {

        if (!(other instanceof RectanglePlus)) {
            return 1; // I'm bigger than any other class!
        }
        RectanglePlus otherRect =(RectanglePlus)other;

        return this.getArea() - otherRect.getArea();
    }
}

或者,第三个选项,您可以将另一个方法添加到您的接口中,以获得对象的可测量的、可实现的值。然后,如果使用的是Java 8,则可以向isLargerThan添加默认实现:

代码语言:javascript
复制
public interface Relatable<T extends Relatable> {

    public default int isLargerThan(T t) {
        return this.getRelatableValue - t.getRelatableValue();
    }

    public int getRelatableValue();

}
票数 1
EN

Stack Overflow用户

发布于 2014-07-17 13:36:25

我不得不承认,您所展示的java文档中的示例非常糟糕,而且令人困惑。这是不好的,因为它包含了一个不安全的层次结构。抛出(从实现类到接口/超类)始终是安全的,但在可能的情况下应该避免强制转换。

理想情况下,Relatable接口还应该包含getArea()方法:

代码语言:javascript
复制
public interface Relatable { 
    public int isLargerThan(Relatable other);
    public int getArea();
}

现在你不需要丑陋的演员了,简单地说:

代码语言:javascript
复制
public int isLargerThan(Relatable other) { 
    if (this.getArea() < other.getArea()) { 
        return -1; 
    } else if (this.getArea () > other.getArea()) { 
        return 1; 
    } else {
        return 0;
    }
}

就够了。我还认为isLargerThan(Relatable other)是个坏名字(在什么方面更大?)它可能应该类似于hasBiggerArea(Relatable other),这样它就可以解释我们实际比较的内容(只有“更大的”才是流行的)。

票数 1
EN

Stack Overflow用户

发布于 2014-07-17 12:04:42

在方法声明public int isLargerThan(Relatable other){...}中,参数other被声明为对其类实现接口Relatable的对象的引用。

在方法体中,表达式(RectanglePlus)other意味着检查对象是否属于RectanglePlus类或该类的子类(如果不是,则抛出ClassCastException )。现在,RectanglePlusRelatable,但相反的情况并不一定是正确的;这里的强制转换确保other要么是RectanglePlus;如果不是,将不会执行进一步的指令,因为会抛出异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24802425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档