Skip to content

Commit

Permalink
modify
Browse files Browse the repository at this point in the history
  • Loading branch information
WuShichao committed Oct 5, 2018
1 parent e6ef04e commit 96aa80f
Showing 1 changed file with 162 additions and 15 deletions.
177 changes: 162 additions & 15 deletions 13.object_oriented_programming.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@

最简单的类(Class)可以通过下面的案例来展示(保存为 `oop_simplestclass.py`):

```text
{% include "./programs/oop_simplestclass.py" %}
```python
class Person:
pass # 一个空的代码块

p = Person()
print(p)
```

输出:

```text
{% include "./programs/oop_simplestclass.txt" %}
$ python oop_simplestclass.py
<__main__.Person instance at 0x10171f518>
```

**它是如何工作的**
Expand All @@ -58,14 +63,22 @@

我们已经在前面讨论过类与对象一如函数那般都可以带有方法(Method),唯一的不同在于我们还拥有一个额外的 `self` 变量。现在让我们来看看下面的例子(保存为 `oop_method.py`)。

```text
{% include "./programs/oop_method.py" %}
```python
class Person:
def say_hi(self):
print('Hello, how are you?')

p = Person()
p.say_hi()
# 前面两行同样可以写作
# Person().say_hi()
```

输出:

```text
{% include "./programs/oop_method.txt" %}
$ python oop_method.py
Hello, how are you?
```

**它是如何工作的**
Expand All @@ -80,14 +93,25 @@

案例(保存为 `oop_init.py`):

```text
{% include "./programs/oop_init.py" %}
```python
class Person:
def __init__(self, name):
self.name = name

def say_hi(self):
print('Hello, my name is', self.name)

p = Person('Swaroop')
p.say_hi()
# 前面两行同时也能写作
# Person('Swaroop').say_hi()
```

输出:

```text
{% include "./programs/oop_init.txt" %}
$ python oop_init.py
Hello, my name is Swaroop
```

**它是如何工作的**
Expand All @@ -110,14 +134,84 @@ _字段(Field)_有两种类型——类变量与对象变量,它们根据

**对象变量(Object variable)**由类的每一个独立的对象或实例所拥有。在这种情况下,每个对象都拥有属于它自己的字段的副本,也就是说,它们不会被共享,也不会以任何方式与其它不同实例中的相同名称的字段产生关联。下面一个例子可以帮助你理解(保存为 `oop_objvar.py`):

```text
{% include "./programs/oop_objvar.py" %}
```python
# coding=UTF-8

class Robot:
"""表示有一个带有名字的机器人。"""

# 一个类变量,用来计数机器人的数量
population = 0

def __init__(self, name):
"""初始化数据"""
self.name = name
print("(Initializing {})".format(self.name))

# 当有人被创建时,机器人
# 将会增加人口数量
Robot.population += 1

def die(self):
"""我挂了。"""
print("{} is being destroyed!".format(self.name))

Robot.population -= 1

if Robot.population == 0:
print("{} was the last one.".format(self.name))
else:
print("There are still {:d} robots working.".format(
Robot.population))

def say_hi(self):
"""来自机器人的诚挚问候
没问题,你做得到。"""
print("Greetings, my masters call me {}.".format(self.name))

@classmethod
def how_many(cls):
"""打印出当前的人口数量"""
print("We have {:d} robots.".format(cls.population))


droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()

droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()

print("\nRobots can do some work here.\n")

print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()

Robot.how_many()
```

输出:

```text
{% include "./programs/oop_objvar.txt" %}
$ python oop_objvar.py
(Initializing R2-D2)
Greetings, my masters call me R2-D2.
We have 1 robots.
(Initializing C-3PO)
Greetings, my masters call me C-3PO.
We have 2 robots.
Robots can do some work here.
Robots have finished their work. So let's destroy them.
R2-D2 is being destroyed!
There are still 1 robots working.
C-3PO is being destroyed!
C-3PO was the last one.
We have 0 robots.
```

**它是如何工作的**
Expand Down Expand Up @@ -172,14 +266,67 @@ how_many = classmethod(how_many)

我们将通过下面的程序作为案例来进行了解(保存为 `oop_subclass.py`):

```text
{% include "./programs/oop_subclass.py" %}
```python
# coding=UTF-8

class SchoolMember:
'''代表任何学校里的成员。'''
def __init__(self, name, age):
self.name = name
self.age = age
print('(Initialized SchoolMember: {})'.format(self.name))

def tell(self):
'''告诉我有关我的细节。'''
print('Name:"{}" Age:"{}"'.format(self.name, self.age), end=" ")


class Teacher(SchoolMember):
'''代表一位老师。'''
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print('(Initialized Teacher: {})'.format(self.name))

def tell(self):
SchoolMember.tell(self)
print('Salary: "{:d}"'.format(self.salary))


class Student(SchoolMember):
'''代表一位学生。'''
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print('(Initialized Student: {})'.format(self.name))

def tell(self):
SchoolMember.tell(self)
print('Marks: "{:d}"'.format(self.marks))

t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 25, 75)

# 打印一行空白行
print()

members = [t, s]
for member in members:
# 对全体师生工作
member.tell()
```

输出:

```text
{% include "./programs/oop_subclass.txt" %}
$ python oop_subclass.py
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)
Name:"Mrs. Shrividya" Age:"40" Salary: "30000"
Name:"Swaroop" Age:"25" Marks: "75"
```

**它是如何工作的**
Expand Down

0 comments on commit 96aa80f

Please sign in to comment.