Create subclass but doesn't need 1 superclass property

1 回表示 (過去 30 日間)
Zhangziyi Zhou
Zhangziyi Zhou 2022 年 9 月 30 日
回答済み: Steven Lord 2022 年 9 月 30 日
My superclass has property [a, b, c]
If I decide to inherite a subclass from it, but doesn't need the superclass property [C],
is there a easier way to achieve it?

回答 (1 件)

Steven Lord
Steven Lord 2022 年 9 月 30 日
  • Ignore the fact that the superclass has the property by not using it in any of the subclass methods.
  • Split the superclass into two classes, one with properties A and B and one with property C. Modify existing subclasses to inherit from both and write your new subclass so it only inherits from the former.
  • Rethink your inheritance hierarchy: if all superclass objects must have property C and your subclass cannot have property C, your subclass would violate the Liskov substitution principle.

カテゴリ

Help Center および File ExchangeSubclass Definition についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by