我正在构建一个包含 3 个类的树结构Node,Leaf并且Branch.
Node是树节点的抽象类。Leaf是从中衍生出来的Node,不能生孩子。Branch派生自Nodewhich can have children and have a methodadd(Node*)
而且我有具体的课程,例如Bookwhich is aLeaf和BookShelfwhich is a Branch。BookShelf但是,如果是 a ,我会加倍,因为当它不是一本书时,Branch该方法add(Node*)无效。Node*因此该Branch接口不适用于BookShelf. 如何处理那些事情?