9

java中的扩展实现在性能和内存等方面有什么区别?例如采取以下场景,

1)

public interface PrintResult
{
  public final int NO_ERROR=0;
  public final int SUCCESS=1;
  public final int FAILED=-1;
}


public class PrintProcess implements PrintResult
{
  //Perform some operation
}



2)

public class PrintResult
{
  public final int NO_ERROR=0;
  public final int SUCCESS=1;
  public final int FAILED=-1;
}


public class PrintProcess extends PrintResult
{
  //Perform some operation
}

对于上述场景(1,2),使用extends(派生子类)和implements(实现接口)有什么区别。在性能、内存等方面。?

4

2 回答 2

8

During a memorable Q&A session, someone asked James Gosling (Java's inventor): If you could do Java over again, what would you change? . I'd leave out classes, he replied

Java does not allow you to extend multiple classes. This avoids some problems related to multiple inheritance. However you can choose to implement multiple interfaces. This is a huge plus.

Some people also believe extends decreases the flexibility of their code.

However, I doubt there is any huge difference in performance, efficiency etc. In fact, they might even produce the same byte-code ( though I'm not sure ). And you are comparing two different things that have different functions, its like asking if Apples are more efficient than Oranges.

于 2012-03-03T12:52:08.273 回答
1

一个类扩展另一个类并实现接口。接口扩展了另一个接口。接口没有任何实现的方法,所有定义的方法都是空的,所以如果类从接口继承,它应该实现它的方法。但是如果 Class1 从 Class2 继承,那么它已经有一些工作方法(来自 Class2)并且只是扩展了 Class2。

于 2015-12-05T12:02:00.577 回答