什么时候使用抽象类或接口?

发布于 2024-07-29 21:54:08 字数 39 浏览 3 评论 0原文

为什么要创建抽象类或接口类,或者什么时候应该使用抽象类或接口类?

Why are abstract or interface classes created, or when should we use abstract or interface classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

書生途 2024-08-05 21:54:19

SamuelCarrijo似乎很好地回答了这个问题。

此外,对于 Java,某些框架需要一个接口才能使用。 我正在考虑(比如说)动态代理,或一些客户端/服务器代理框架。 这是因为它们使用对象的内省来确定由对象实现的接口实现的方法。 因此,有时您必须为对象实现一个接口,而您通常不会这样做。

注意这个接口的原因是Java特有的。

SamuelCarrijo seems to have answered this question well.

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.

Note this reason for interfaces is specific to Java.

柒七 2024-08-05 21:54:18

当您构建继承层次结构时,将使用抽象类。 然而,大多数继承层次结构不应该太“深”(即继承层次太多)。 许多面向对象的设计书籍都倾向于接口而不是继承(我读过的一本书曾经引用一位开发人员的话说“继承是您不会实现的最酷的[面向对象]功能”),因为这允许为类分配行为”通过契约”,其中契约是接口。

值得注意的是 samuelcarrijo 的答案 - 如果你想要一个方法的默认实现,你必须使用一个具有该方法的具体实现的抽象类来为其提供默认实现。 可以在子类中覆盖此默认实现。

希望这可以帮助!

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.

Hope this helps!

雨落□心尘 2024-08-05 21:54:16

请参阅接口基本上是一个“合同”。 当你定义一个接口时,你就定义了一个契约。 在扩展抽象类的地方,实现接口。

让我们考虑一个例子。

public interface Friend {
void hello();
}

现在您已经定义了一个契约,其中规定任何想要实现 Friend 的类都需要提供方法 hello() 的定义。

这是一个实现:

public class myFriend implements Friend {
public void hello()
println("Done");
}

现在 myFriend 已经履行了合同。 现在的问题是:接口应该用在哪里?

接口帮助您定义必须实现的行为。 假设您有一个 A 类,它定义了一些功能。 您希望其他类仅在定义特定行为(方法)时才使用此类功能。 您可以根据接口强制执行此限制。

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().

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?

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.

太阳男子 2024-08-05 21:54:15

抽象类是一个类,它至少有一个抽象方法,或者您也可以将所有方法设置为抽象方法。 显然它不能被实例化。 您必须从抽象类继承并在继承类(即扩展抽象类的类)中实现抽象方法。

接口根本不是类(所以不要称它们为接口类)。 接口定义方法的签名,但没有任何实现。 接口也没有成员字段。 如果在类中实现接口,则必须为该接口提供的所有方法提供实现。

为某些东西定义一个通用的 API 是有意义的,它们可以有完全不同的实现。 抽象类对于那些基本相同但有一些细微差别的类更有用。 您可以结合使用这两种方法。

一个很好的例子是 集合框架 Java 类库。 您拥有 List 接口,它定义了列表的行为方式。 一些实现例如 ArrayList 和 LinkedList。 由于它们的行为相似,所以对两者工作相同的东西是在抽象类 AbstactList 中实现的,两者都继承了它。

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.

A good example is the collections framework of 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.

π浅易 2024-08-05 21:54:13

主要区别在于您可以在一个类中实现多个接口,但只能扩展单个抽象类。 这是因为抽象类还可以定义存储数据的字段,而接口则不能。

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.

绝影如岚 2024-08-05 21:54:12

当您只想声明类必须具有哪些方法和成员时,可以使用接口。 任何实现该接口的人都必须声明并实现该接口列出的方法。

如果您还想有一个默认实现,请使用抽象类。 扩展抽象类的任何类都必须仅实现其抽象方法和成员,并且将具有抽象类的其他方法的一些默认实现,您可以覆盖也可以不覆盖。

--编辑-忘记提及,Earwicker 提醒我

最后,您可以实现任意数量的接口,但只能扩展一个类(无论是否抽象)。 选择之前请记住这一点。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文