Python学习笔记(二十五)- 面向对象编程 总览(OOP-The Big Picture)

1. Python中OOP的要点是什么?
答:OOP是关于代码重用的 - 您可以通过自定义已存在的内容而不是更改代码或从头开始来最小化代码冗余(redundancy)和程序。

2.继承搜索(inheritance)在哪里查找属性?
答:继承搜索首先在实例对象中查找属性,然后在实例创建的类中,然后在所有更高级的超类中,从对象树的底部到顶部,从左到右(通过默认)。搜索在找到属性的第一个位置时候停止。由于在此过程中找到的名称的最低版本获胜,所以类自然支持在新的扩展中通过扩展进行定制。

3.类对象和实例对象有什么区别?
答:类和实例对象都是命名空间【namespace】(作为属性出现的变量包)。它们之间的主要区别在于类是一种用于创建多个实例的工厂。类还支持运算符重载(operator overloading)方法(实例继承),并将嵌套在类中的任何函数视为处理实例的方法。

4.为什么类的方法函数中的第一个参数是特殊的?
答:类的方法函数中的第一个参数是特殊的,因为它总是接收实例对象,该实例对象是方法调用的隐含主体。通常按惯例称为self。因为默认情况下方法函数总是具有这个隐含的主体和对象上下文,所以我们说它们是“面向对象的”(即,设计用于处理或改变对象)。

5. __init__方法用于什么?
答:如果__init__方法在类中被编写或继承,Python每次创建该类的实例时都会自动调用它。它被称为构造方法(constructor method);它会隐式传递新实例,以及显式传递给类名的任何参数。它也是最常用的运算符重载方法。如果不存在__init__方法,则实例只会以空命名空间的形式开始它的生命周期。

6.如何创建类的实例?
答:通过调用类名来创建类实例,就像它是一个函数一样;传递给类名的任何参数在__init__构造函数方法中显示为两个参数或者更多。新实例会记住为继承目的而创建的类。

7.你如何创建一个类?
答:通过运行class语句创建一个类;与函数定义一样,这些语句通常在导入封闭模块文件时运行(下一章将详细介绍)。

8.如何指定某个类的超类?
答:通过在类状态的括号中将它们列在新类的名称之后,指定类的超类。从括号中列出类的从左到右的顺序给出了类树中从左到右的继承搜索顺序。

一些其它的东西:

1.OOP:Object-oriented Pragramming 面向对象编程  do things with stuffs

2.class 类:粗鲁地说,使用和处理内建对象类型的函数包 

3.类是定义新事物的方法,反映了程序领域的真实对象

4.class与instance是不同的对象类型

class:作为实例工厂,它们的属性提供行为(数据与函数)

instance:表示程序中具体的项(每个对象记录不同的数据)

5.就搜索树(search tree)而言,实例属性来自它的类,类属性来自树中它上面的所有类。树中较高的类被称为超类(superclass)或者是基类(base class);较低的类被称为子类(subclass)或者是派生类(derived class)。这些术语说的都是相对树中位置和角色。

6.假设我们要使用 I2.w,会从I2及以上开始搜索,比方说就是在 I2,C1,C2,C3中查找属性。结果如果在C3.w找到我们要的属性,我们就称I2继承C3的属性w

7.class与instance

最主要的区别:class是一种生成实例的工厂

事实上,面向对象的模型与传统的程序+记录的数据处理模型并没有什么不同

class:

(1)每个class语句生成新的class对象

(2)每次class被调用,生成一个新的instance对象

(3)实例被创建之后自动连接到类

(4)类根据我们在类标题行中(header line)的括号里列出它们的方式自动链接它们的超类;在搜索树中以从左到右的顺序。

8.出现在class里的def语句,被称为方法(method),接受一个特别的第一个参数(self),提供要处理的实例的句柄

9.如果你使用过c++或者java,Python的self与this类似

10.__init__ 构造器(constructor)构造函数

11.polymorphism(多态):代码不应该关心对象是什么,而应该关心它做什么

12.OOP模型:(1)在对象树中查找属性

                        (2)带有特别函数参数(self)

注:转载《Learning Python 5th Edition》[奥莱理]
1. What is the main point of OOP in Python?
2. Where does an inheritance search look for an attribute?
3. What is the difference between a class object and an instance object?
4. Why is the first argument in a class's method function special?
5. What is the __init__ method used for?
6. How do you create a class instance?
7. How do you create a class?
8. How do you specify a class's superclasses?

1. OOP is about code reuse—you factor code to minimize redundancy and program by customizing what already exists instead of changing code in place or starting from scratch.
2. An inheritance search looks for an attribute first in the instance object, then in the class the instance was created from, then in all higher superclasses, progressing from the bottom to the top of the object tree, and from left to right (by default). The search stops at the first place the attribute is found. Because the lowest version of a name found along the way wins, class hierarchies naturally support customization by extension in new subclasses.
3. Both class and instance objects are namespaces (packages of variables that appear as attributes). The main difference between them is that classes are a kind of factory for creating multiple instances. Classes also support operator overloading methods, which instances inherit, and treat any functions nested in the class as methods for processing instances.
4. The first argument in a class's method function is special because it always receives the instance object that is the implied subject of the method call. It's usually called self by convention. Because method functions always have this implied subject and object context by default, we say they are “object-oriented” (i.e., designed to process or change objects).
5. If the __init__ method is coded or inherited in a class, Python calls it automatically each time an instance of that class is created. It's known as the constructor method; it is passed the new instance implicitly, as well as any arguments passed explicitly to the class name. It's also the most commonly used operator overloading method. If no __init__ method is present, instances simply begin life as empty namespaces.
6. You create a class instance by calling the class name as though it were a function; any arguments passed into the class name show up as arguments two and beyond in the __init__ constructor method. The new instance remembers the class it was created from for inheritance purposes.
7. You create a class by running a class statement; like function definitions, these statements normally run when the enclosing module file is imported (more on this in the next chapter).
8. You specify a class's superclasses by listing them in parentheses in the class statement, after the new class's name. The left-to-right order in which the classes are listed in the parentheses gives the left-to-right inheritance search order in the class tree.
 

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/88189904