.NET面试之面向对象(一)

面向对象的基本原则

一。Encapsulate things in your design that are likely to change(封装变化)

  We isolate the parts that will change a lot into a sparate class or classes that we can depend on changing, and we increase our flexibility and ease of change. We also leave the stable part of our design alone, so that we just need to implement them once and test them once.(将变化的部分提取到单独的类,将稳定的部分保存在单独的类)

二。Code to an Interface rather than an implementation(面向接口编程而不是面向具体实现)

     http://wenku.baidu.com/link?url=mPtltPKNn9MueYDA5TvFsmhUB3aGeKjoFbv8KC2Ji-AzrZmKlGeCGWnqkeIekE_FR0oeyLRZajlvELjdnAfuTvNPCJXVeLO2FdWodcvVrZ7 

三。The open-closed principle(开闭原则)

  Classes should be open for extention and close for modification(对扩展开放,对修改关闭)

     Not only for classes, but also for methods.

四。Don‘t repeat yourself principle(避免重复代码)

五。The single responsibilty principle(单一职责原则)

  A class should have one, and only one reason to change.

六。Liskov substitution principle(李氏置换原则)

  Subclasses must be substitutionable for their base class.(父类必须能被子类替代)

     getting the behavior of derived classes right and thus getting the inheritance right.

  Indications that you‘re violation LSP include:

  1. A subclass doesn‘t keep all the external observable behavior of its super class.

  2. A subclass modifies, rather than textends, the external observalble behavior of its super class.

  3. A subclass throws exceptions in an effort to hide certain behavior defined in its super class.

  4. A subclass that overrides a virtual method defined in its super class using an empty implementation in order to hide certain behavior defined in its super class.

  5. Method overrideing in derived classes is the beggest cause of LSP violations.

七。Dependency inversion principle(依赖倒置原则)

  1. High-level modules should not depend on low-level modules. Both should depend on abstraction.

  2. Abstractions should not depend on details. Details should depend on abstractions.

八。The interface segregation principle(接口分离原则)

  Clients should not have to depend on interfaces they don‘t use. In particular, they shouldn‘t have to depend on methods they don‘t use.

  Instead of adding new methods that are only appropriate to one or a few implementation classes,you make a new interface.

九。The principle of least knowledge(最少知道原则)Law of Demeter(迪米特法则)

  Talk only to your immediate friends.

  The principle of least knowledge is all about loose coupling. Classes should collaborate indirectly as few other classes as possible.

.NET面试之面向对象(一),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。