日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Interface classes

發(fā)布時(shí)間:2024/4/15 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Interface classes 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

An interface class is a class that has no members variables, and where all of the functions are pure virtual!

(接口類是一個(gè)沒(méi)有任何成員變量并且所有的函數(shù)都是純虛函數(shù)的類。)

?In other words, the class is purely a definition, and has no actual implementation.

? Interfaces are useful when you want to define the functionality that derived classes must implement, but leave the details of how the derived class implements that functionality entirely up to the derived class.

Interface classes are often named beginning with an I. Here’s a sample interface class:

class IErrorLog {virtual bool OpenLog(const char *strFilename) = 0;virtual bool CloseLog() = 0;virtual bool WriteError(const char *strErrorMessage) = 0; };

Any class inheriting from IErrorLog must provide implementations for all three functions in order to be instantiated.

(任何繼承IErrorLog的派生類都必須實(shí)現(xiàn)這三個(gè)方法才可以被實(shí)例化。)

You could derive a class named FileErrorLog, where OpenLog() opens a file on disk, CloseLog() closes it, and WriteError() writes the message to the file. You could derive another class called ScreenErrorLog, where OpenLog() and CloseLog() do nothing, and WriteError() prints the message in a pop-up message box on the screen.

Now, let’s say you need to write some code that uses an error log. If you write your code so it includes FileErrorLog or ScreenErrorLog directly, then you’re effectively stuck using that kind of error log. For example, the following function effectively forces callers of MySqrt() to use a FileErrorLog, which may or may not be what they want.

double MySqrt(double dValue, FileErrorLog &cLog) {if (dValue < 0.0){cLog.WriteError("Tried to take square root of value less than 0");return 0.0;}elsereturn dValue; }

A much better way to implement this function is to use IErrorLog instead(多態(tài)):

double MySqrt(double dValue, IErrorLog &cLog) {if (dValue < 0.0){cLog.WriteError("Tried to take square root of value less than 0");return 0.0;}elsereturn dValue; }

Now the caller can pass in any class that conforms to the IErrorLog interface.

?If they want the error to go to a file, they can pass in an instance of FileErrorLog. If they want it to go to the screen, they can pass in an instance of ScreenErrorLog. Or if they want to do something you haven’t even thought of, such as sending an email to someone when there’s an error, they can derive a new class from IErrorLog (eg. EmailErrorLog) and use an instance of that! By using IErrorLog, your function becomes more independent and flexible.

Interface classes have become extremely popular because they are easy to use, easy to extend, and easy to maintain. In fact, some modern languages, such as Java and C#, have added an “interface” keyword that allows programmers to directly define an interface class without having to explicitly mark all of the member functions as abstract. Furthermore, although Java and C# will not let you use multiple inheritance on normal classes, they will let you multiply inherit as many interfaces as you like. Because interfaces have no data and no function bodies, they avoid a lot of the traditional problems with multiple inheritance while still providing much of the flexibility.


URL:http://www.learncpp.com/cpp-tutorial/126-pure-virtual-functions-abstract-base-classes-and-interface-classes/



總結(jié)

以上是生活随笔為你收集整理的Interface classes的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。