这几天从头看《Thinking in C++》,感觉的确是本好书,现在根据自己的理解将其中的要点进行整理,希望让它变成自己脑子里的东西。
1. 几个关键词的中英文对照
2. Alan Kay 总结了Smalltalk的五个基本特点,这几个特点代表了纯粹的面向对象编程的方法。
3. 面向对象的三个基本特征:封装、继承、多态
封装:The hidden implementation
封装的好处:
1. 向使用者隐藏实现,这样可以在不影响使用者的情况下改变后面的实际实现。
Because if it's hidden, the client programmer can't use it, which means that the class creator can change the hidden portion at will without worrying about the impact to anyone else.
2. 不让类的使用者误操作一些不该他操作的东西,可以减少出错的机会。
To keep client programmers' hands off portions they shouldn't touch-parts that are necessary for the internal machinations of the data type but not part of the interface that users need in order to solve their particular problems.
3. 让使用者更专注于他所需要的功能。(自己理解)
继承:Inheritance
It's nicer if we can take the existing class, clone it, and then make additions and modifications to the clone. This is effectively what you get with inheritance.
重用已经存在的类,对它进行添加或修改。
based type/derived type(基类、派生类)
You have two ways to differentiate your new derived class from the original base class. The first is simply add brand new functions to the derived class. The second and more important way is to change the behavior of an existing base-class function. This is referred to as overriding that function.
由两种方法使派生类与基类不同:一种是在派生类中添加新的方法,一种是改变基类中已有的方法,即覆盖。
第一种方法是is-like-a,第二种方法是is-a
多态:polymorphism
多态的一个经典的例子,多态个人理解就是在函数的参数里使用基类,但实际调用的时候可以传由此基类派生的子类,这样一个操作根据传递的参数不同可以有不同的效果。
void doStuff(Shape& s) { s.erase(); s.draw(); } Circle c; Triangle t; Line l; doStuff(c); doStuff(t); doStuff(l);
4. public, private and protected
5. Composition组合
重用类的一种方法是组合,即将别的类的对象作为另一个类的成员来使用。
you can place an object of that class inside a new class. Your new class can be made up of any number and type of other objects.
有什么理解的不对的地方请大家不吝赐教!
另:封装的英文单词是什么呀?encapsulation?