Design Hint for Inheritance(继承设计的一些小贴士)

1.Place common operations and fields in the superclass.

  把一些基本的操作和熟悉放到父类之中去

2.Don’t use protected fields.

  protected 的熟悉,可以使得其他包中的类在继承此类以后可以直接访问这个类的属性,破坏了java的封装性;

  而且在同一个包的非子类也可以随意的访问类的属性;

  但是protected 的方法可以暗示这个方法没有准备好正常使用,而应该在子类中重写!

3.Use inheritance to model the “is–a” relationship.

  应该在满足“is-a”的关系中才用继承,不然最后子类会多出很多无用的属性和方法,代码写下了会过渡冗余

4.Don’t use inheritance unless all inherited methods make sense.

  只有当所有继承的方法有意义的时候才使用继承!

5.Don’t change the expected behavior when you override a method.

  当你重写方法时,不要修改方法的期望行为

6.Use polymorphism, not type information.

  使用多态,而不是类型判断;

7.Don’t overuse reflection.

  不要过度使用反射,编译器没办法伴你找到问题所在

posted @ 2018-04-18 16:36  Eric_Jin  阅读(75)  评论(0编辑  收藏  举报