抽象类和接口有什么区别?
可能的重复:
接口与抽象类(一般面向对象)
我可以实例化抽象类吗?如果是这样,为什么我不将所有非密封类抽象化?
如果我不能实例化它,那么它和接口有什么区别?抽象类可以具有“基”类功能吗?接口和抽象类之间还有更多的区别吗?
Possible Duplicate:
Interface vs Abstract Class (general OO)
Can I ever instantiate an Abstract class? If so, why would I not make all my non-sealed classes abstract?
If I can't instantiate it, then what is the difference from an interface? Can the abstract class have "base" class functionality? Is there more to the difference between an interface and an abstract class than that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法实例化抽象类。
抽象类和接口之间的区别在于,抽象类可以有方法的默认实现,因此如果不在派生类中重写它们,则使用抽象基类实现。接口不能有任何实现。
You can't instantiate an abstract class.
The difference between an abstract class and an interface is that an abstract class can have a default implementation of methods, so if you don't override them in a derived class, the abstract base class implementation is used. Interfaces cannot have any implementation.
接口不提供实现。您还可以实现多个接口。
您可以在抽象类内部提供实现,但只能从一种基类型继承。
无论哪种情况,您都不能直接实例化其中任何一个。
Interfaces don't provide an implementation. You can also implement multiple interfaces.
You can provide an implementation inside of an abstract class, but you can only inherit from one base type.
In either case, you can't directly instantiate either one.
您不能直接创建抽象类的实例。但是,您可以提供方法和/或属性实现,这是在接口中无法做到的。此外,您只能继承一个类(抽象类或其他类),而您可以继承(实现)任意数量的接口。
You cannot directly create an instance of an abstract class. You can, however, provide method and/or property implementations, which you cannot do in an interface. Also you can only inherit one class, abstract or otherwise, whereas you can inherit (implement) as many interfaces as you like.
抽象类:
抽象类可以包含抽象方法和非抽象方法
抽象方法。
当非抽象类继承自抽象类时
类,非抽象类应该提供所有
继承的抽象方法的实现。
接口:
接口只不过是纯抽象类,即
接口只能包含函数声明。
接口的所有成员默认都是Public的
并且您不能提供任何访问修饰符。
当一个类从接口继承时,
继承的类应该提供实际的实现
继承的成员。
Abstract Class:
Abstract Class Can contain Abstract Methods and Non-
Abstract Methods.
When a Non-Abstract Class is Inherited from an Abstract
Class, the Non-Abstract Class should provide all the
implementations for the inherited Abstract Method.
Interface:
Interface is nothing but Pure Abstract Class ie
Interface can contain only the function declaration.
All the members of the interface are Public by Default
and you cannot provide any access modifiers.
When a class is inherited from the interface, the
inherited class should provide actual implementations for
the inherited members.
一方面:您只能从一个抽象类继承。您可以将多个接口附加到一个类。
For one thing: You can only inherit from one abstract class. You can have multiple interfaces attached to a class.
接口定义了一个契约,您说您将保证一组特定方法签名的限制。
抽象类仅意味着您不能直接创建它的新实例,您可以自由使用任何其他类功能,即属性。
Interface defines a contract, your saying that you will guantee implimitations of a specific set of method signatures.
An abstract class just means you can't directly create a new instance of it, your free to use any other class features, ie properties.