Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个基类 Fruit 和一个子类 Apple 的实例。我应该如何将 Apple 实例保存到数据库中?通过检查来检测 Apple的insertFruit方法instanceof是否被认为是好的设计?
insertFruit
instanceof
我认为在超类中有一个方法来检查对象是否是子类的实例不是一个好的设计。如果您想稍后添加香蕉怎么办?你会在 insertFruit() 中添加另一个检查吗?
我认为最好使用多态性,并覆盖 Apple 类中的 insertFruit() 方法。