C++中接口的使用
在C++中使用接口会使用户代码和实现代码完全分离,在.h文件声明抽象方法,在.cpp文件实现方法。例如:
接口类
//TestClass.h
class test{
public :
void a();
};
实现方法
//TestClass.cpp
#include<iostream>
#include"TestClass.h"
using namespace std;
//通过::(二元作用域分辨运算符)将
void test::a(){
cout<<"yes";
}
int main(){
test c;
c.a();
}
此时调用抽象类的方法结果就是调用了实现之后的方法,实现了用户代码和实现代码的彻底分离