在Java中,我们可以在不实例化的情况下声明接口变量。这可以帮助我们处理一些抽象的概念。在下面的Java示例中,我定义了一个接口UShape
和两个UShape
类Rectangle
和Triangle
。在测试类bucket中,我能够定义一个名为myshape
的私有接口变量,并使用该变量进行操作。
interface UShape {
void setwidth(int i);
void setheight(int i);
int getarea();
}
class Rectangle implements UShape{
private int w, h;
@Override
public void setwidth(int i) {
this.w = i;
}
@Override
public void setheight(int i) {
this.h = i;
}
@Override
public int getarea() {
return this.w * this.h;
}
}
class Triangle implements UShape{
private int w, h;
@Override
public void setwidth(int i) {
this.w = i;
}
@Override
public void setheight(int i) {
this.h = i;
}
@Override
public int getarea() {
return this.w * this.h / 2;
}
}
class bucket{
private UShape myshape;
//do something with myshape
public void defineShape(int i){
if (i == 1){
myshape = new Rectangle();
myshape.setwidth(5);
myshape.setheight(5);
}
}
public void printArea(){
System.out.println(myshape.getarea());
}
}
public class TestShape {
public static void main(String[] args){
bucket b = new bucket();
b.defineShape(1);
b.printArea();
}
}
我现在的问题是,在C++中,我们如何实现这个程序?我检查了一下,发现C++中的抽象类不能用来声明像UShape myshape
这样的变量。我们可以使用其他方法来实现一个类似类的带有接口变量的bucket吗?
也许我误解了你的问题,但你没有理由不能声明抽象类类型的私有成员。以下内容应该与您的Java示例类似:
class UShape {
public:
virtual void setWidth (int w) =0;
virtual void setHeight (int h) =0;
virtual int getArea() =0;
virtual ~UShape() =0;
};
class Rectangle: public UShape {
private:
int width;
int height;
public:
void setWidth (int w) { this.width = w; }
void setHeight (int h) { this.height = h; }
int getArea (void) { return (width * height); }
};
class Triangle: public UShape {
private:
int width;
int height;
public:
void setWidth (int w) { this.width = w; }
void setHeight (int h) { this.height = h; }
int getArea (void) { return (width * height) / 2; }
};
class bucket{
private:
std::unique_ptr<UShape> myshape;
public:
//do something with myshape
void defineShape(int i){
if (i == 1){
myshape = std::make_unique<Rectangle>();
myshape->setWidth(5);
myshape->setHeight(5);
}
}
void printArea(){
cout << myshape ? myshape->getArea() : 0;
}
}
int main () {
bucket b();
b.defineShape(1);
b.printArea();
}
我将使用与Mike相同的想法,但使用C++11和C++14原语进行内存管理,即在不显式调用delete运算符的情况下清理分配给指针的内存。
class UShape {
public:
virtual void setWidth (int w) =0;
virtual void setHeight (int h) =0;
virtual int getArea() =0;
};
class Rectangle: public UShape {
private:
int width;
int height;
public:
void setWidth (int w) { this.width = w; }
void setHeight (int h) { this.height = h; }
int getArea (void) { return (width * height); }
};
class Triangle: public UShape {
private:
int width;
int height;
public:
void setWidth (int w) { this.width = w; }
void setHeight (int h) { this.height = h; }
int getArea (void) { return (width * height) / 2; }
};
class bucket{
private:
std::unique_ptr<UShape> myshape; //c++11
public:
//do something with myshape
void defineShape(int i){
if (i == 1){
myshape = std::make_unique<Rectangle>(); //C++14
myshape->setWidth(5);
myshape->setHeight(5);
}
}
void printArea(){
cout << myshape->getArea();
}
}
int main () {
std::unique_ptr<bucket> b(new bucket()); //c++11
b->defineShape(1);
b->printArea();
}
在C++中,您必须使用指向基类的指针,或者更好的是指向智能指针,如shared_ptr
或unique_ptr
:
class bucket{
private:
std::unique_ptr<UShape> myshape;
public:
//do something with myshape
void defineShape(int i){
if (i == 1){
myshape = std::make_unique<Rectangle>();
myshape->setwidth(5);
myshape->setHeight(5);
}
}
void printArea(){
std::cout << myshape->getarea() << std::endl;
}
};
int main() {
bucket b;
b.defineShape(1);
b.printArea();
}
在某些情况下,参考资料也很有用:
Rectangle r;
UShape& shape = r;
std::cout << shape.getarea() << std::endl;