为什么我要在Java中使用这种情况下的接口?

时间:2011-02-28 22:15:57

标签: java oop interface

我正在尝试理解Java OOP概念的基础知识,所以我对接口提出了一个问题,因为它让我感到困惑。下面我正在玩两节课。一个实现 SizeComparable 接口,另一个实现 SizeComparable 接口。

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),但我仍然不确定这一点。对不起,如果问题有点愚蠢,我只想完全理解。

4 个答案:

答案 0 :(得分:7)

如果您有InterestingBoringIndifferentCrazy类,它们都代表一些高度可比的对象,那么所有这些类都可以实现{{1}接口,因此可以相互比较。

如果没有接口,您需要在每个类中使用n个方法将其与自身和所有其他类进行比较。

答案 1 :(得分:1)

一开始它可能没有多大意义,但是当你开始注入依赖项,开始测试或者会编写多个接口实现时,它会真正给你提升。

它还允许多重继承。有时你想要类似的东西 - 非常通用的界面,可以被系统中的很多类使用。这将伴随着更大的系统和更大的类层次结构。

现在只需信任java世界的其余部分,并使用它们接口:)

祝你好运

答案 2 :(得分: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缺乏多重继承。

答案 3 :(得分: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实现不受此影响。

这里的好处是,现在例如排序算法可以对实现SizeComparable的任何类型X的列表/数组进行排序,因此您不必为可能要按高度排序的每种新类型的对象再次编写它。

(事实上,标准API中已经存在类似的界面Comparable<X>。也许您想要使用此而不是SizeComparable。)

顺便说一句,对于isXXX方法,通常布尔返回类型比整数更明智。