与接口和抽象类的对象创建混淆 [英] confuse with object creation of interface and abstract Class

查看:54
本文介绍了与接口和抽象类的对象创建混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对接口和抽象类的对象创建非常困惑,请给我任何人清楚吗?

i am very confuse with object creation of interface and abstract class please give me anyone clarity for this?

推荐答案

你确实很困惑:你无法创建一个 abstract 类或接口的对象:这就是重点。



当你将一个类声明为 abstract 时,你特意告诉编译器这是一个我希望需要额外信息的类 - 不要允许创建此类的对象,但确保所有派生类都实现以下方法

例如,Apples和Oranges都是Fruit:所以你可以声明一个抽象的Fruit类,并派生出来它的Apple和Orange类都有。但你可以在商店购买苹果,同样也可以在Orange购买。但如果您尝试购买水果,您将获得非常有趣的外观 - 因为它不作为单个项目存在,它仅作为其衍生消耗品的特定实例存在!



接口类似,但它们不是类 - 您根本无法创建它们!接口是合同:如果你实现这些方法,那么你可以加入这个俱乐部 - 有点像注册Codeproject实现了ICodeprojectMember接口 - 它允许你发布问题。除非您实施必要的方法(通过提供唯一的用户名,密码和活动的电子邮件地址),否则您尚未实现ICodeprojectMember接口,因此您无法发布。网站编译器拒绝让你做任何事情,直到你完成了你的合同。
You are indeed confused: you can't create an object of either an abstract class or an interface: that is the whole point.

When you declare a class as abstract you are specifically telling the compiler "this is a class that I expect to need extra information - do not allow objects of this class to be created, but do ensure that all derived classes implement the following methods"
For example, Apples and Oranges are both Fruit: so you could declare an abstract Fruit class, and derive both the Apple and the Orange class from it. But you can buy an Apple in the shops, ditto an Orange. But you will get very funny looks if you try to buy a Fruit - because it does not exist as an individual item, it only exists as the specific instance of its "derived" consumables!

Interfaces are similar, but they aren't classes - and you can't create them at all! Interfaces are contracts: "If you implement these methods, then you can join this club" - a bit like signing up for Codeproject implemented the ICodeprojectMember interface - which allows you to post questions. Unless you implement the necessary methods (by providing a unique username, a password, and an active email address) you haven't implemented the ICodeprojectMember interface, so you can't post. The site "compiler" refuses to let you do anything until you have fulfilled your side of the contract.


请看我对这个问题的评论。请仔细考虑并尝试弄清楚您的顾虑。



同时,请查看我过去的答案并阅读所有讨论:

如何决定选择抽象类或接口 [ ^ ],

抽象类和接口之间的区别,如果它们具有相同的no方法和var [ ^ ],

当我们使用抽象,当我们使用界面...? [ ^ ],

接口和多态性 [ ^ ],

没有过载和超载的多态性可能 [ ^ ],

对接口的怀疑 [ ^ ]。



欢迎您提出后续问题。



-SA
Please see my comment to the question. Please think at it and try to figure out what are your concerns.

In the meanwhile, please see my past answers and read all the discussion:
How to decide to choose Abstract class or an Interface[^],
Difference between abstract class and interface if they have same no of methods and var[^],
When we use abstract and when we use interface...?[^],
Interfaces and Polymorphism[^],
POLYMORPHISM WITHOUT OVERLOADING AND OVERRRIDING IS POSSIBLE[^],
Doubts on Interfaces[^].

Your follow-up questions will be welcome.

—SA


这篇关于与接口和抽象类的对象创建混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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