想了解C#和C ++中的接口 [英] Want to know about Interfaces in C# and C++

查看:103
本文介绍了想了解C#和C ++中的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么接口要使用?
您可以提供实现吗?

Why Interfaces use ?
Can u give an Implementation ?

推荐答案

接口在C#中提供了一种实现运行时polymorphism的方法.使用接口我们可以通过相同的Interface引用从不同的类调用函数,而使用virtual functions我们可以通过相同的引用从相同的继承层次结构中的不同类调用函数.
参考更多: C#中的接口(面向初学者) [ http://msdn.microsoft.com/en-us/library/ms173156.aspx [ ^ ]

从C ++迁移到C#所需的知识 [何时使用界面 [ C#中的接口 [ ^ ]

从C ++实现用C#声明的接口 [ ^ ]

以下线程描述:
  • 了解接口的用途.
  • 定义接口.
  • 使用接口.
  • 实现接口继承.
Interfaces in C # provide a way to achieve runtime polymorphism. Using interfaces we can invoke functions from different classes through the same Interface reference, whereas using virtual functions we can invoke functions from different classes in the same inheritance hierarchy through the same reference.
Refer more: Interfaces in C# (For Beginners)[^]

Interfaces describe a group of related functionalities that can belong to any class or struct. You define an interface by using the interface keyword.
Refer: http://msdn.microsoft.com/en-us/library/ms173156.aspx[^]

What You Need to Know to Move from C++ to C#[^]
When To Use Interfaces[^]
Interfaces in C#[^]

Implementing an interface declared in C# from C++[^]

Following thread describes:
  • Understand the Purpose of Interfaces.
  • Define an Interface.
  • Use an Interface.
  • Implement Interface Inheritance.


在C#中,您只能从单个基类继承. C ++与众不同,因此我不再赘述,以免引起混淆.

因此,您有一个需要从UserControl派生的类.很好,您可以在类定义中继承UserControl,然后就可以使用-您具有可以向其添加其他控件并进行显示的控件.
In C# you can only inherit from a single base class. C++ is different, so I won''t talk about that, so as not to confuse.

So you have a class which needs to derive from UserControl. Fine, you inherit UserControl in your class definition and off you go - you have a control you can add other controls to, and display.
class MyClass : UserControl
   {
   }

如果您还希望将此控件用在foreach循环中,因为它是数据库中的项目列表,该怎么办?为此,您需要实现一些方法,否则foreach将无法正常工作.但是,如何告诉编译器您的类实现了必需的方法?很简单,您将IEnumerable接口添加到类定义中:

What if you also want this control to be used in a foreach loop, because it is a list of items in a database? For that, you need to implement a few methods or foreach won''t work. But how do you tell the compiler that your class implements the required methods? Simple, you add the IEnumerable interface to your class definition:

class MyClass : UserControl, IEnumerable<string>
   {
   }

现在,为了编译代码,您必须添加所有必需方法的定义:

Now, in order for your code to compile, you have to add definitions of all the required methods:

public IEnumerator<string> GetEnumerator() { ... }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { ... }

完成合同后,您可以在foreach中使用您的类:

When you do you have completed your side of the contract, and you can use your class in a foreach:

private void xxx()
    {
    MyClass mine = new MyClass();
    foreach (string s in mine)
        {
        }
    }

而且,如果您按预期的方式编码(多数情况下这很容易),那么一切都将起作用.

这就是界面的含义:您与界面用户之间的合同.如果您遵守规则,则可以成为俱乐部会员,并获得俱乐部提供的任何好处.

And, if you code them as you are supposed to (which is pretty easy most of the time) it will all work.

That is what an Interface is: a contract between you and the interface user. If you follow the rules, you can be a member of the club, and get whatever benefits the club provides.



这篇关于想了解C#和C ++中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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