Visual C ++接口问题-在最终在类本身中定义了所有方法之后,接口的需求是什么? [英] Visual C++ Interfaces Problem - After all the methods are finally to be defined in the class itself, then whats the need of interface?

查看:76
本文介绍了Visual C ++接口问题-在最终在类本身中定义了所有方法之后,接口的需求是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Visual C ++的初学者.我正在研究接口.我什么都没有.
假设

I am beginner to Visual C++. I am working on interfaces. I didn''t got one thing..
Suppose

//Problem Details

interface Shape 
{
    virtual void Draw();
};

interface Rectangle : Shape 
{
    virtual void Draw();
};

class Maths : Rectangle
{
public:  
    void Draw() {
        //Code for Draw
    }
};

//This was my problem
-----------------------------------------------------------
//We can just write the above whole code as...

class Maths
{
public:
    void Draw() {
        //Code for Draw
    }
};


-------------------------------------------------- -----------

我有自己的Draw()方法使用Math类.如果我们可以在第二种情况下编写上面提到的代码,那么需要什么接口.

所有方法最终都将在类本身中定义之后,那么接口的需要是什么...?


-------------------------------------------------------------

I have class Maths with its own Draw() method. If we can write the above code as mentioned above in second case, then what the need of interfaces.

After all the methods are finally to be defined in the class itself, then whats the need of interface...?

推荐答案

您需要了解一些概念.一个是在此处找到的MSDN文章:根据接口编程 [ ^ ]和阅读多态 [依赖注入 [
You need to understand a couple of concepts for this. One is a MSDN article found here: Programming against Interfaces[^] and reading up on polymorphism[^] would also help.


Just a small hint at what interfaces will help you with: The application using interfaces will not have to be aware of the concrete implementation of a class. This is especially true if dependency injection[^] is used to construct the application at runtime.
[/Edit]

Cheers!

--MRB


纯C ++中不存在接口"的概念和关键字.它仅适用于Java和COM(也许还有其他一些,我怀疑在CLI中运行的任何东西都支持这种类型的东西,但我不确定). MS定义基于结构(这几乎是一个没有实例化的裸类).

阅读更多有关它的信息:
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Interface_Class [ ^ ]
在C ++中使用接口 [ http://www.codeguru.com/forum/archive/index.php/t- 241695.html [ ^ ]
http://social.msdn.microsoft.com /Forums/zh-CN/vclanguage/thread/06bf1dea-1d20-4ec3-b9a1-3d673d7fcd8d/ [
The concept and keyword of "interface" doesn''t exist in pure C++. It''s only native to Java and COM (and maybe a few others, I suspect anything that runs in the CLI would support this type of thing but I''m not sure). The MS definition is struct based (which is pretty much a bare class with no instantiation).

Read a little more about it:
http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Interface_Class[^]
Using Interfaces in C++[^]
http://www.codeguru.com/forum/archive/index.php/t-241695.html[^]
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/06bf1dea-1d20-4ec3-b9a1-3d673d7fcd8d/[^]


首先,关键字interface是特定于Microsoft的,并且仅在托管C ++中使用,因此它不是C ++标准的一部分.

其次,interface不能单独使用,必须编写interface classinterface struct(等效).

第三,从技术上讲,您可以完全省略interface关键字,而无需更改代码的语义! MS发明了在类定义中强制某些属性的拐杖.

最后,要了解定义接口类的含义,您实际上必须了解多态性和面向对象编程背后的基本概念.您要问的实际上是面向对象的基础,因此,在前进之前,您应该尝试搜索关于该主题的好书,在线课程或教程.解决方案2中已经有一些有用的链接,但是它们可能对您来说太高级或太具体了.

您最好从面向对象的编程基础开始,例如
本教程.
First of all, the keyword interface is Microsoft-specific, and only used in managed C++, so this is not part of the C++ standard.

Second, interface can''t be used standalone, you have to write interface class or interface struct (which are equivalent).

Third, technically, you can entirely omit the interface keyword without changing the semantics of the code! It is just a crutch that MS invented to enforce certain properties in a class definition.

Lastly, to understand the meaning of defining interface classes, you really have to understand polymorphism and the basic concepts behind object-oriented programming. What you''re asking is really the basis of object orientation, so you should try and search for a good book, online course, or tutorial on that topic before advancing. There are already a few useful links in solution 2, but they may already be too advanced for you, or too specific.

You''d better get started with the basis of object oriented programming, such as this tutorial.


这篇关于Visual C ++接口问题-在最终在类本身中定义了所有方法之后,接口的需求是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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