-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode01_object.py
More file actions
141 lines (91 loc) · 2.33 KB
/
code01_object.py
File metadata and controls
141 lines (91 loc) · 2.33 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 类的定义
class Hero(object):
name = "Hero" # 属性
def info(self): # self表示实例本身(相当于Java的this?)
print("I am the class Hero")
# 创建对象
hero = Hero()
print(hero.name) # Hero
hero.info() # I am the class Hero
class Student():
name = "Peter"
def info(self):
print("xxx")
print(Student.__dict__) # 查看类的属性
print(Student.__dict__["name"]) # 查看单个属性
print(Student.name) # 查看属性
Student.age = 18 # 新增属性
Student.name = "Lucy" # 修改属性
del Student.age # 删除属性
class Order:
def __init__(self, id, costprice, sellprice):
self.id = id
self.costprice = costprice
self.sellprice = sellprice
def getprofit(self):
return self.sellprice - self.costprice
class Person:
def __init__(self, name):
self.name = name
def __del__(self):
print(f"{self.name}被销毁了")
person = Person("kk")
del person
order = Order(1, 32.1, 68.2)
print(f"订单 {order.id} 的利润是{order.getprofit()}")
class A:
_name = "LL"
__age = 18 # 私有属性
def get_age(self):
return self.__age
def set_sex(self, sex):
self.__sex = sex
def get_sex(self):
return self.__sex
def __printfoo(self): # 私有方法
print("foo")
def printsth(self):
self.__printfoo() # 调用私有方法
print("sth.")
a = A()
print(A._name) # LL
# print(A.__age) # AttributeError: type object 'A' has no attribute '__age'. Did you mean: '_A__age'?
print(a._name) # LL
# print(a.__age) # AttributeError: 'A' object has no attribute '__age'. Did you mean: '_A__age'?
print(a.get_age()) # 正确访问私有属性的方式
a.printsth()
a.set_sex("男")
print(a.get_sex())
class A1:
def printa1(self):
print("a1")
class B(A, A1):
pass # 什么都不写
b = B()
b.printsth()
b.printa1()
class C:
def printc(self):
print("c")
class C1(C):
def printc1(self):
print("c1")
class C2(C1):
pass
c2 = C2()
c2.printc()
c2.printc1()
class Animal:
def say(self):
print("")
def eat(self):
print("Eat meat")
class Dog(Animal):
def say(self):
print("Wangwang")
def eat(self):
super().eat()
print("Eat noodle")
dog = Dog()
dog.say()
dog.eat()