2011-02-28 79 views
1

我想了解Java OOP概念的基础知识,所以我有一个关于接口的问题,因为它让我有点困惑。下面我正在玩两个班。其中一个实现了SizeComparable接口,另一个则不工作。为什么我应该在Java中使用这种接口?

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

+0

一个接口允许有人从头开始实现你的接口,或者在原始或主要目的与你的接口完全不同的其他代码中实现你的接口。 – yogsma 2011-02-28 22:23:17

回答

7

如果你有InterestingBoringIndifferentCrazy类,所有代表的一些对象的高度相当,然后所有的人都可以实现SizeComparable接口,因此可以相互媲美。

没有接口,你需要每个类中的n个方法来比较它与自身和所有其他方法。

+1

这四个中哪一个是你*? :)(记住,Java不允许多重继承!) – corsiKa 2011-02-28 22:19:18

+0

但是行:*有趣的otherInteresting =(有趣的)obj; *将对象转换为有趣的。我如何将它转换为Boring,Crazy或其他对象将使用getHeight()方法的方式? – Rihards 2011-02-28 22:22:14

+4

@Richards - 你必须在界面中定义'getHeight()'。 – Bozho 2011-02-28 22:23:30

1

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

此外它允许多重继承。有时候你想要类似可比较的东西 - 非常通用的接口,可以被系统中的很多类使用。这将伴随着更大的系统和更大的班级层次。

Java世界的现在只相信休息,并使用这些接口:)

和好运

+0

的答案谢谢你的帮助! :) – Rihards 2011-02-28 22:26:45

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缺乏多重继承。

+0

这与* SomeOtherClass.isHigher()*方法混淆我比我以前更多。 Heheh。 – Rihards 2011-02-28 22:57:21

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实现并未受到影响。

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

(事实上,已经有类似的界面Comparable<X>标准的API中。也许你要改用您SizeComparable这一点。)

顺便说一句,对于isXXX方法通常是一个布尔返回类型为比整数更合理。

+0

是的,我同意,* isSomething *应该返回布尔值,但我只是用0和1来轻松打印出来。但是,谢谢你,会尽力完全理解它。 – Rihards 2011-03-01 00:36:14

相关问题