【发布时间】:2018-04-30 14:52:51
【问题描述】:
有没有办法将接口方法的许多签名分配给一个实现?
例如,任何ITransformable 都可以通过矢量或三倍坐标移动:
public interface class ITransformable
{
public:
void move(double x, double y, double z);
void move(Vector ^ offset);
};
这种表示法要求程序员在每个子类中实现这两种方法,但其中只有一个具有有用的主体,其他的只会引用第一个类似
public ref class Thing : public ITransformable
{
virtual void move(double x, double y, double z)
{
//Each child implements it in it's own way
...
}
virtual void move(Vector ^ offset)
{
//It is the same for all childs, copy it and paste everywhere
move(offset->x, offset->y, offset->z);
}
}
有没有类似的东西:
public interface class ITransformable
{
public:
//Implement me
void move(double x, double y, double z);
//Need no overriding anymore, just use implementation of the method above
void move(Vector ^ offset) : move(offset->x, offset->y, offset->z);
};
没有多重继承(假设Thing继承了一些非接口类,所以ITransformable不能是抽象类)。
【问题讨论】:
-
这是不可能的,只有抽象基类可以做到这一点。实现接口的类中的单行方法不会让你慢很多。
标签: inheritance interface c++-cli overloading clr