forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04.inheritance.py
More file actions
83 lines (63 loc) · 1.91 KB
/
04.inheritance.py
File metadata and controls
83 lines (63 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""
@description: 继承
@author:XuMing
"""
from __future__ import print_function # 兼容python3的print写法
from __future__ import unicode_literals # 兼容python3的编码处理
# 类定义的基本形式:
# class ClassName(ParentClass):
# """class docstring"""
# def method(self):
# return
# 里面的 ParentClass 就是用来继承的
class Clothes(object):
def __init__(self, color="green"):
self.color = color
def out_print(self):
return self.__class__.__name__, self.color
# Test:
my_clothes = Clothes()
print(my_clothes.color)
print(my_clothes.out_print())
# 定义一个子类,继承父类的所有方法
class NikeClothes(Clothes):
def change_color(self):
if self.color == "green":
self.color = "red"
# 继承父类的所有方法:
your_clothes = NikeClothes()
print(your_clothes.color)
print(your_clothes.out_print())
# 但有自己的方法
your_clothes.change_color()
print(your_clothes.color)
# 如果想对父类的方法进行修改,只需要在子类中重定义这个类即可:
class AdidasClothes(Clothes):
def change_color(self):
if self.color == "green":
self.color = "black"
def out_print(self):
self.change_color()
return self.__class__.__name__, self.color
him_clothes = AdidasClothes()
print(him_clothes.color)
him_clothes.change_color()
print(him_clothes.color)
print(him_clothes.out_print())
# super() 函数
# super(CurrentClassName, instance)
#
# 返回该类实例对应的父类对象。
# 刚才 AdidasClothes可以改写为:
class NewAdidasClothes(Clothes):
def change_color(self):
if self.color == "green":
self.color = "black"
def out_print(self):
self.change_color()
print(super(NewAdidasClothes, self).out_print())
# Test
her_clothes = NewAdidasClothes()
print(her_clothes.color)
her_clothes.out_print()