forked from HaoZhang95/Python24
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic01.py
More file actions
109 lines (84 loc) · 2.65 KB
/
basic01.py
File metadata and controls
109 lines (84 loc) · 2.65 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
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
"""
如果在类的外面取值和修改私有属性的值,需要自己定义get/set方法
"""
class Person(object):
def __init__(self):
self.name = "老王"
self.__age = 20
def get_age(self):
return self.__age
def set_age(self, age):
self.__age = age
p = Person()
print(p.name)
p.name = "王五"
print(p.name)
print(p.get_age())
p.set_age(28)
print(p.get_age())
"""
多态,因为python是弱引用类型,并不关注参数的类型,所以需要自己打出obj.eat()的方法
"""
"""
1- 实例属性就是对象属性, 和类属性不是一回事
2- 对象创建的属性叫做实例属性, 类中的属性叫做类属性
3- 如果想使用类属性,直接大写Student.num即可, 或者s1.num
4- 但是修改类属性的值只能通过Student.num
5- 类方法需要添加修饰器@classmethod, 每个对象都自动拥有的方法,只有一份的属性和方法
"""
class Student(object):
# 类属性,每个创建的对象都有这个num的属性,不会为类属性单独的开辟多份内存
# 多个对象共享一份类属性,修改的话,每个对象的num都会更改,全变了
# 如果想在类的外面修改类私有属性,使用类方法
num = 0
__sex = "Male"
@classmethod
def get_sex(cls):
return cls.__sex
@classmethod
def set_sex(cls, sex):
cls.__sex = sex
def __init__(self, name, age, country = "中国"):
self.name = name
self.age = age
self.country = country
s1 = Student("小明", 22)
s2 = Student("小红", 28)
s1.city = "石家庄"
print(s1.city) # 实例属性
print(Student.num) # 类属性
print(s1.num)
Student.num = 200
print(s1.num)
print(s2.num)
print(Student.get_sex())
Student.set_sex("Female")
print(Student.get_sex())
print(s1.get_sex())
s1.set_sex("Male")
print(Student.get_sex())
"""
静态方法,并没有self或者cls
类中存在的方法只有三个, 类方法, 对象方法, 静态方法
1- 如果在类的内部想使用self --> 对象方法
2- 如果在类的内部想使用cls --> 类方法
3- 如果在类的内部不使用self和cls --> 静态方法
"""
class Teacher(object):
__country = "中国"
def __init__(self):
self.__age = 10
# 类方法-用来读写类属性的
@classmethod
def get_country(cls):
return cls.__country
# 对象方法
def get_age(self):
return self.__age
# 静态方法- Teacher.say_hello()或者t1.say_hello()都可以访问
@staticmethod
def say_hello():
print("Hello Python.")
Teacher.say_hello()
t1 = Teacher
t1.say_hello()