-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend.py
115 lines (81 loc) · 3.03 KB
/
extend.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
# encoding: utf-8
# @author: hoojo
# @email: hoojo_@126.com
# @github: https://github.com/hooj0
# @create date: 2018-04-02 22:21:06
# @copyright by hoojo@2018
# @changelog Added python3 `object -> extend` example
class Person:
''' 定义基本属性 '''
name = 'lucy'
age = 22
''' 定义私有属性,外部无法访问 '''
__height = 166
def __init__(self, name, age, height):
self.name = name
self.age = age
self.__height = height
''' 定义方法,访问私有属性和基本属性 '''
def info(self):
print('I am name is %s, age %s , height %s' % (self.name, self.age, self.__height))
# 继承Person 对象
class Student(Person):
# 新的属性
grade = ''
def __init__(self, name, age, height, grade):
# 为父类属性赋值
Person.__init__(self, name, age, height)
# 为当前类属性赋值
self.grade = grade
# 覆盖父类方法
def info(self):
# 子类无法访问父类私有方法
# print('I am name is %s, age %s , height %s, grade: %s' % (self.name, self.age, self.__height, self.grade))
print('I am name is %s, age %s, grade: %s' % (self.name, self.age, self.grade))
# 自定义的其他方法
def getMyGrade(self):
print('my grade: %s' % self.grade)
#######################################################
stu = Student('jackson', 21, 178, '3年级')
stu.info()
stu.getMyGrade()
#######################################################
class Classes:
className = ''
classNo = ''
def __init__(self, name, no):
self.className = name
self.classNo = no
def info(self):
print('I am is %s class, class No %s' % (self.className, self.classNo))
def getClass(self):
print('your class %s' % self.className)
# 继承多个类
class ClassStudent(Classes, Student):
classStudentNo = ''
def __init__(self, name, age, height, grade, className, no, classStudentNo):
# 调用父类构造函数
Classes.__init__(self, className, no)
# 调用父类构造
Student.__init__(self, name, age, height, grade)
# 为当前类属性赋值
self.classStudentNo = classStudentNo
# 覆盖父类方法
def info(self):
Classes.info(self)
Student.info(self)
# 其他方法
def other(self):
Classes.getClass(self)
Student.getMyGrade(self)
##################################################################
cs = ClassStudent('jack', 25, 199, '3班', 'C301', '三年级', 'C301030303')
cs.info()
cs.other()
# 访问 父类属性
print(cs.className)
print(cs.name)
# 私有属性无法方法
#print(cs.__height)
##################################################################