このケースについて疑問に思っています。従業員は人から継承し、マネージャーは従業員から継承します。どちらのステートメントが正しいでしょうか。
Person alice = new Employee();
Employee bob = new Person();
Manager cindy = new Employee();
Manager dan = (Manager)(new Employee());
スケッチを描きましょう:
Person
Employee (Employee can be though as a subset of Person)
Manager (Manager can be though as a subset of Employee)
つまり、すべて Manager
がEmployee
、すべて Employee
がPerson
です。シーケンスは推移的であるため、すべて Manager
がPerson
です。さあ、見てみましょう
Person alice = new Employee();
Employee
インスタンス(new Employee();
)を作成し、それをPerson
(Person alice =
)インスタンスに割り当てようとしました。以来、すべて Employee
であるPerson
あなたがそれを行うことができます。
それどころか:
Manager cindy = new Employee();
Employee
インスタンスを作成し、それをManager
インスタンスに割り当てようとしました。すべて Employee
がであるという規則がないため、それを行うことはできませんManager
(まったく逆です:すべてManager
はですがEmployee
、その逆はありません)。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加