你如何使用抽象类和接口之间做出选择? [英] How do you decide between using an Abstract Class and an Interface?

查看:201
本文介绍了你如何使用抽象类和接口之间做出选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

复制:<一href="http://stackoverflow.com/questions/56867/interface-vs-base-class">http://stackoverflow.com/questions/56867/interface-vs-base-class


我已经越来越深入到面向对象,设计模式,和ActionScript 3的世界,我还是好奇,怎么知道什么时候使用抽象类(假的AS3不支持抽象类)和接口。对我来说,都只有作​​为模板,确保一定的方法在一个给定的类中实现。完全是区别在抽象类需要继承和接口只是延长的事实?

I have been getting deeper into the world of OOP, design patterns, and actionscript 3 and I am still curious how to know when to use an Abstract class (pseudo for AS3 which doesn't support Abstract classes) and an interface. To me both just serve as templates that make sure certain methods are implemented in a given class. Is the difference solely in the fact that Abstract classes require inheritance and an Interface merely extends?

谢谢, 布赖恩·霍奇 hodgedev.com

Thanks, Brian Hodge hodgedev.com

推荐答案

使用抽象类,如果你有,你想要它的子类有一些功能。举例来说,如果你有一组你希望所有的抽象基类的子类有功能。

Use an abstract class if you have some functionality that you want it's subclasses to have. For instance, if you have a set of functions that you want all of the base abstract class's subclasses to have.

如果你只是想对行为/功能的总承包使用的接口。如果你有,你要带一组不同的对象的函数或对象,使用接口。然后你就可以变出传入的对象,而不改变方法或对象时服用。

Use an interface if you just want a general contract on behavior/functionality. If you have a function or object that you want to take in a set of different objects, use an interface. Then you can change out the object that is passed in, without changing the method or object that is taking it.

接口通常是松散的,比较抽象类。你不会希望使用接口的情况下,你在不断地写相同的$ C $下所有的接口方法。使用一个抽象类,定义每一个方法一次。

Interfaces are typically loose, compared to Abstract classes. You wouldn't want to use interfaces in a situation where you are constantly writing the same code for all of the interface's methods. Use an abstract class and define each method once.

另外,如果你想创建一个特定对象的继承层次,你真的不会想尝试这样做,只有接口。

Also, if you are trying to create a specific object inheritance hierarchy, you really wouldn't want to try to do that with just interfaces.

另外,同样,在一些语言中,你只能有一个基类,如果对象已经有一个基类,你将不得不做一些重构,以使用抽象基类。这可能会或可能不会意味着你可能要使用一个inteface代替。

Also, again, in some languages you can only have a single base class, and if an object already has a base class, you are going to have to do some refactoring in order to use an abstract base class. This may or may not mean that you might want to use an inteface instead.

由于@tvanfosson指出,这不是一个坏主意,使用了大量的接口,当你真正理解抽象类和接口,这是不是一个真正的要么/或情况。特定情况下可以同时使用抽象类和接口或两者都不是。我喜欢用接口,有时干脆来限制哪些方法或对象可以在传入的参数对象访问。

As @tvanfosson notes, it's not a bad idea to use a lot of interfaces, when you really understand abstract classes and interfaces, it's not really an either/or situation. A particular situation could use both abstract classes and interfaces or neither. I like to use interfaces sometimes simply to restrict what a method or object can access on a passed in parameter object.

这篇关于你如何使用抽象类和接口之间做出选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆