Java 什么时候使用抽象类或接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1221512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use abstract class or interface?
提问by JavaResp
Why are abstract or interface classes created, or when should we use abstract or interface classes?
为什么要创建抽象类或接口类,或者什么时候应该使用抽象类或接口类?
采纳答案by Samuel Carrijo
Interface is used when you only want to declare which methods and members a class MUST have. Anyone implementing the interface will have to declare and implement the methods listed by the interface.
当您只想声明类必须具有哪些方法和成员时使用接口。任何实现接口的人都必须声明和实现接口列出的方法。
If you also want to have a default implementation, use abstract class. Any class extending the abstract class will have to implement only its abstract methods and members, and will have some default implementation of the other methods of the abstract class, which you may override or not.
如果您还想拥有默认实现,请使用抽象类。任何扩展抽象类的类都必须仅实现其抽象方法和成员,并且具有抽象类其他方法的一些默认实现,您可以覆盖或不覆盖这些方法。
--EDIT - forgot to mention, Earwicker reminded me
--EDIT - 忘了提,Earwicker 提醒我
Finally, you can implement as many interfaces as you want, but only extend one class (being it abstract or not). Keep that in mind before choosing.
最后,您可以实现任意数量的接口,但只能扩展一个类(无论是否抽象)。在选择之前请记住这一点。
回答by Mnementh
An abstract class is a class, that has atleast one abstract method or you can also make all your methods as abstract. Obviously it cannot be instantiated. You have to inherit from an abstract class and implement the abstract methods in the inheriting class (i.e, the class extending the abstract class).
抽象类是一个类,它至少有一个抽象方法,或者您也可以将所有方法设为抽象。显然它不能被实例化。您必须从抽象类继承并在继承类(即扩展抽象类的类)中实现抽象方法。
Interfaces are not classes at all (so don't call them interface class). Interfaces define the signature of methods without any implementation. Also interfaces have no member-fields. If you implement an interface in a class, you have to provide implementations for all the methods provided by the interface.
接口根本不是类(所以不要称它们为接口类)。接口定义了没有任何实现的方法的签名。接口也没有成员字段。如果在类中实现接口,则必须为该接口提供的所有方法提供实现。
It makes sense to define a generalized API for some stuff, that can have completely different implementations. Abstract classes are more useful for classes that do mainly the same, but have some subtle differences. You can combine both approaches.
为某些东西定义一个通用的 API 是有意义的,它可以有完全不同的实现。抽象类对于基本相同但有一些细微差别的类更有用。您可以结合使用这两种方法。
A good example is the collections frameworkof the Java class-library. You have the interface List, that defines how Lists have to behave. Some implementations are for instance ArrayList and LinkedList. As they behave similar, the stuff that works the same for both is implemented in the abstract class AbstactList, both inherit this.
一个很好的例子是Java 类库的集合框架。你有接口 List,它定义了 Lists 的行为方式。一些实现是例如 ArrayList 和 LinkedList。由于它们的行为相似,因此在抽象类 AbstactList 中实现了对两者相同的东西,两者都继承了这一点。
回答by Scott Davies
Abstract classes are used when you are building an inheritance hierarchy. However, most inheritance hierarchies should not be too "deep" (i.e. too many levels of inheritance). Many object oriented design books will favor interfaces over inheritance (one book I read once quoted a developer as saying that "inheritance is the single coolest [object oriented] feature you won't implement"), as this allows classes to be assigned behaviors "by contract", where the contract is the interface.
在构建继承层次结构时使用抽象类。但是,大多数继承层次结构不应太“深”(即继承层次太多)。许多面向对象的设计书籍更喜欢接口而不是继承(我读过的一本书曾引用一位开发人员的话说“继承是你不会实现的最酷的[面向对象]特性”),因为这允许为类分配行为“按合同”,其中合同是接口。
It is worth noting samuelcarrijo's answer - if you want to have a default implementation of a method, you would have to use an abstract class that has a concrete implementation of the method to give it a default implementation. This default implementation can be overridden in child classes.
值得注意的是 samuelcarrijo 的回答——如果你想要一个方法的默认实现,你必须使用一个具有该方法具体实现的抽象类来给它一个默认实现。这个默认实现可以在子类中被覆盖。
Hope this helps!
希望这可以帮助!
回答by Brian Agnew
SamuelCarrijoseems to have answered this question well.
SamuelCarrijo似乎很好地回答了这个问题。
In addition for Java, some frameworks require an interface to work with. I'm thinking of (say) dynamic proxies, or some client/server proxying frameworks. This is because they use introspection on the object to determine methods implemented by the interfaces implemented by the object. So occasionally you have to implement an interface for an object where, perhaps, you wouldn't normally bother.
除了 Java 之外,一些框架需要一个接口才能使用。我在考虑(比如说)动态代理,或者一些客户端/服务器代理框架。这是因为它们使用对对象的自省来确定由对象实现的接口实现的方法。因此,有时您必须为一个对象实现一个接口,而在这种情况下,您通常不会打扰。
Note thisreason for interfaces is specific to Java.
请注意,接口的这个原因是特定于 Java 的。
回答by Daniel Earwicker
The key difference is that you can implement
multiple interfaces in a class, but only extend
a single abstract class. This is because an abstract class can also define fields that store data, whereas an interface cannot.
关键区别在于您可以implement
在一个类中使用多个接口,但只能extend
使用一个抽象类。这是因为抽象类还可以定义存储数据的字段,而接口则不能。
回答by Duleb
See Interface is basically a "Contract". When you are defining an interface you are defining a Contract. Where abstract classes are extended, interfaces are Implemented.
请参阅接口基本上是一个“合同”。当您定义接口时,您就是在定义合同。在抽象类被扩展的地方,接口被实现。
Let's consider an example.
让我们考虑一个例子。
public interface Friend {
void hello();
}
Now you have defined a contract which says that any class which wants to implement Friend
needs to provide a definition for method hello()
.
现在你已经定义了一个契约,它说任何想要实现的类都Friend
需要为 method 提供一个定义hello()
。
Here is an implementation:
这是一个实现:
public class myFriend implements Friend {
public void hello()
println("Done");
}
Now myFriend
has fulfilled the contract. Now the question is: Where should interfaces be used?
现在myFriend
已经履行了合同。现在的问题是:应该在哪里使用接口?
Interfaces help you define a behavior which must be implemented. Say you have a class A which defines some functionality. You want the other classes to use this class functionality only if they define particular behavior (methods). You enforce this restriction in term of interface.
接口帮助您定义必须实现的行为。假设您有一个 A 类,它定义了一些功能。您希望其他类仅在定义特定行为(方法)时才使用此类功能。您在接口方面强制执行此限制。