-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.py
More file actions
43 lines (37 loc) · 1.07 KB
/
02.py
File metadata and controls
43 lines (37 loc) · 1.07 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
# coding=utf-8
# 类
class Student(object):
#定义类属性
name = 'Felix'
age = 23
#变量名两个下划线开头,定义私有属性,这样在类外部无法直接进行访问,类的私有方法也无法访问
__sex = 0
#定义构造方法
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.__sex = sex
#类方法
def get_sex(self):
return self.__sex
def set_sex(self,sex):
self.__sex = sex
#调用
if __name__ == '__main__':
student = Student('Felix',12,1) #实例化v成员变量
print(student.age,student.name)
#单继承
class TopStudent(Student):
top_id = 1024
def __init__(self,name,age,sex,top_id):
#调用父类的构造方法
super(TopStudent,self).__init__(name,age,sex)
self.top_id = top_id
#重写父类方法
# def set_sex(self,sex):
# self.__sex = sex+1
# print('重写父类的方法')
#调用
if __name__ == '__main__':
topStudent = TopStudent('Felix1',45,0,2048)
print(topStudent.set_sex(1))