什么是好的代码, 什么是好的设计?
首先,好的设计需要一个完整的functionality,并且是能够被容易使用和扩展的。
为什么要用面对对象编程?
1.有一些数据,总是要用一个类似的结构储存,用类似的方法去处理。比如食物的卡路里总是要以HashMap的方式,存入String和对应的热量(Integer), 那么就可以用HashMap加其中的API.
2)暴露有限的接口, 可以调用但不知道如何实现
List<Integer> temp = new ArrayList<Integer>,比如在call list的时候, list.size(), list.get()并不知道其中是如何实现的。
3)重用程序,根据不同情况进行扩展。 List ——> AbstractList
4) Modular Testing
OOP优点
Object Oriented Programming is to code based on objects, build modular, reusable and understandable system. There are several advantages of OOP, such as encapsulation, allows the coder to hide data and methods; Inheritance, based on different situation, coder can extend the class and make evolvement; and Polymorphism where the programmer can implements different methods based on different objects.
In short, easy to use, to hide, to evolve.
Difference between Interface and Abstract Class
Abstract class provides a common base class implementation to derived class, while Interface focus on implementing API signature.
An object can implement multiple interface but can only extend one abstract class. beyond that, an object needs to implemenets all methods in the interface.
extends a class: is a
implements an interface: has an ability
网友评论