C#哎呀与界面有关的概念问题 [英] C# Oops concept question related to interface

查看:87
本文介绍了C#哎呀与界面有关的概念问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们使用接口以及何时使用它?

Why we use interface and when should we use it?

推荐答案

基本上,接口是一种在C#中提供某种多重继承的方法。



因为你只能从一个基类派生一个类(或者事情开始变得非常混乱),没有它就很难提供额外的功能。一个基类描述了一个类是什么 - 接口描述一个类可以做什么。



他们提供一个合同:如果你实现了所需的位,你可以得到对待作为我们中的一员。例如,如果您的Students类派生自People,并实现IEnumerable,那么它具有People的所有功能,但它也可以在foreach循环中使用。



在类实现相同的接口的情况下,它们以相同的方式调用所有,并且通过相同的方法集,尽管是不相关的类,否则。



另见: http ://msdn.microsoft.com/en-us/library/87d83y5b(v = vs.80).aspx [ ^ ]

和这里:http://en.wikibooks.org/wiki/C_Sharp_Programming/Interfaces [ ^ ]







因此,如果有必要定义实现接口的类中的每一件事,那么为什么我们需要接口,我们只能声明和定义该类中的所有东西。这是主要的疑问我必须在实现类中定义每个方法和变量,然后我们可以在该类中声明这些变量,然后才需要接口rite?



编号:笑:

让我们暂时忽略电脑。



当我去大学(很长一段时间)这就是所谓的封闭式商店 - 加入学生会(我所加入的唯一一个工会)是法律要求,直到你加入(并支付年费,不要忘了)联盟会费)你不能参加讲座,你不能加入联盟酒吧(他们出售便宜的啤酒,因为它是一个会员俱乐部),你不能加入任何大学社交俱乐部。因为所有这些都要求您使用学生会会员卡登录,以证明您有权成为会员:您就读于正确的大学。您所在的国家/地区很可能存在类似情况。



如果您将UnionMembership视为接口,则不会传达任何信息。没有任何好处。我的学生班必须实施UnionMembership以获得它的属性:UnionMembership.MemberCard - 并执行它的方法:PayFeeEveryYear(双倍)。

但我的学生必须是一个人(其中包括更多)属性和方法)其中许多学生班不需要影响:姓名,地址,社会安全号码,父母,......

但你不能从两个不相关的类继承!可以理解的是,决定这太复杂了,在实践中给出了太多问题。



接口让我们无法从两个类中派生出来,而是从基类(Person)派生并包含其他类的成员资格的方法,并获得此类成员所带来的所有好处:

Basically, interfaces are a way to provide some kind of multiple inheritance in C#.

Because you can only derive a class from a single base class (or things start to get very confusing) it is difficult to provide additional features without it. A Base class describes what a class is - Interfaces describe what a class can do.

They provide a contract: "If you implement the required bits, you can be treated as one of us". For example, if your Students class is derived from People, and implements IEnumerable, then it has all the features of People, but it can also be used in a foreach loop.

Where classes implement the same interface, they call all be used in the same way, and through the same method set, despite being unrelated classes otherwise.

Also see here: http://msdn.microsoft.com/en-us/library/87d83y5b(v=vs.80).aspx[^]
and here: http://en.wikibooks.org/wiki/C_Sharp_Programming/Interfaces[^]



"So if it is necessary to define each and every thing in the class that implements interface then why do we need interface,we can declare and define all the things in that class only. This is the main doubt i have as we have to define each method and variables in the implementing class then we can declare these variables in that class only then there is no need of interface rite?"

No. :laugh:
Let's ignore computers for a moment.

When I went to University (a long time ago) it was what is called a "closed shop" - it was a legal requirement to join the Students Union (the one and only labour union I have ever joined) and until you had joined (and paid the annual fee, lets not forget the Union Dues) you couldn't attend lectures, you couldn't join the Union Bar (where they sold cheap beer as it was a membership club), you couldn't join any of the University social clubs. Because all of them required you to sign on with your Student Union membership card to prove you had a right to be a member: you attended the right university. There is very likely something similar in your country.

If you think of UnionMembership as an Interface it conveyed nothing. Provided no benefits. My Student class had to implement the UnionMembership to get it's Property: UnionMembership.MemberCard - and execute it's method: PayFeeEveryYear(double lots).
But my student had to be a Person (which includes a lot more properties and methods) many of which the Student class doesn't need to affect: Name, address, social security number, parents, ...
But you can't inherit from two unrelated classes! Understandably, it was decided that this was way too complex and gave too many problems in practice.

An Interface gives us not a way to derive from two classes, but instead a way to derive from a base class (Person) and include a membership of another class, and to get all the benefits that such a membership confers:
public class Student : Person, IUnionMembership
   {
   ...
   }





这有意义吗?



Does that make sense?


这篇关于C#哎呀与界面有关的概念问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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