继承与组合有什么区别? [英] What is the difference between inheritance and composition?

查看:62
本文介绍了继承与组合有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,两者的含义使我难以理解.

As title says, the meaning of both eludes me.

推荐答案

继承表示一个is-a关系,而组合表示两个类之间的has-a关系.

Inheritance expresses a is-a relationship, while composition expresses a has-a relationship between the two classes.

多边形的一个例子是多边形.它具有点的有序序列.用C ++术语:

An example for composition is a polygon. It has a ordered sequence of Points. In C++ terms:

struct Polygon {
  std::vector<Point> points;
};

logic_errorexception时:

struct logic_error : public exception {
};

这篇关于继承与组合有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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