Python学习(是我的课堂上学到的东西)不包含课外

2023-11-07课堂

下面是今天学的有关python的一些内容
归纳一下就是对Python的类,对象,魔术方法

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
"""
创建类和对象
"""

# 类和对象
# 1.静态特征
# 2.动态行为

# 属性
class Person:
name = None
age = None
friends = None
# 构造函数
def __init__(self, name, age, friends):
self.name = name
self.age = age
self.friends = friends
# 行为
def introduce(self):
print(f"大家好,我是{self.name},今年{self.age}岁,我朋友有{self.friends}")

# 定义对象
obj = Person("lichungan", 40 , ["hey", "you","hello"])

obj.introduce()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 定义学生类
class Student:
# 成员变量
name = None
# 成员方法
def say_hi(self):
print(f"Hello, I am{self.name},happy")

# 创建类对象
stu1 = Student()
stu1.name = "lvzhitian"
stu1.say_hi()

# 创建类对象第二个
stu2 = Student()
stu2.name = "lvzhitian2"
stu2.say_hi()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
创建对象:构造函数
"""

class Student:
def __init__(self,name,age,address):
self.name = name
self.age = age
self.address = address
print("已经创建一个学生对象,并且做了初始化")

stu1 = Student("lvzhitian",20,"Hongkong")
print(f"{stu1.name}{stu1.age}{stu1.address}")
stu2 = Student("lvzhitian2",30 ,"Macau")
print(f"{stu2.name}{stu2.age}{stu2.address}")
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
"""
魔术方法
"""

class Student:
def __init__(self,name,age,address):
self.name = name
self.age = age
self.address = address
print("已经创建一个学生对象,并且做了初始化")

# __str__:默认输出类和内存地址
def __str__(self):
print("__str__(self)")
return f"{self.name}{self.age}{self.address}"

# 默认进行<,>会报错,所以需要魔术方法__lt__
def __lt__(self,other):
print("__lt__(self,other)")
return self.age < other.age

# 默认进行<=,>=会报错,所以需要魔术方法__le__
def __le__(self,other):
print("__le__(self,other)")
return self.age <= other.age

# 默认==是进行比较两个对象的内存地址
def __eq__(self,other):
print("__eq__(self,other)")
return self.address == other.address

stu1 = Student("lvzhitian",20,"Hongkong")
stu2 = Student("lvzhitian2",30,"Macau")

# 默认调用的是__str__魔术方法
print(stu1)
print(str(stu1))

# 默认不能进行<,>比较
print(stu1 < stu2)
print(stu1 > stu2)

# 默认不能进行<=,>=比较
print(stu1 <= stu2)
print(stu1 >= stu2)

# 默认==比较的是两个对象
stu3 = stu1
stu4 = Student("lvzhitian3",40,"Taiwan")
print(stu1 == stu2)
print(stu1 == stu3)
print(stu1 == stu4)
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
"""
其他常用的魔术方法
"""

class Test:
# __new__:实例化类时,自动调用,用于创建类的新实例
def __new__(cls,*args,**kwargs):
print("__new__(cls,*args,**kwargs)")
print("new object")
# 必要时,调用父类创建对象方法
return super().__new__(cls)

# __init__:实例化类时,自动调用,用于初始化类的新实例
def __init__(self,*args,**kwargs):
print("__init__(self,*args,**kwargs)")
print("init object")
super().__init__(*args,**kwargs)

# __del__:销毁对象时,自动调用,用于释放资源,但是python会在执行完毕后,自动释放资源,所以其实也可以不用去管它
def __del__(self):
print("del object")

# __call__:调用类时自动调用,用于调用类的方法
def __call__(self,*args,**kwargs):
print("__call__(self,*args,**kwargs)")
print("call object")

# 创建并初始化实例
t = Test()
# 将实例当作函数调用
t()
# 销毁用例
del t
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
class Staff:
def __init__(self,id,name,dep,salary):
self.id = id
self.name = name
self.dep = dep
self.salary = salary
print("已经创建一个员工对象,并且做了初始化")

def work(self,time):
if time > 8 and time < 18:
if time >12 and time < 14:
self.eat()
else:
print(f"我是员工:{self.name},我在划水")
else:
self.sleep()

def eat(self):
print(f"我是员工{self.name}吃饭")

def sleep(self):
print(f"我是员工{self.name}摸鱼")

s = Staff(1,"lvzhitian","it",10000)

s.work(12)

# 测试
for i in range(8,20):
s.work(i)
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
import switch

accounts = {
"11111111":80000,
"22222222":100
}
class ATM:
def query_money(self,account):
if accounts[account]:
print(f"{account},有{accounts[account]}")
return accounts[account]
def set_money(self,account,money):
yuer = None
if accounts[account]:
accounts[account] += money
print(f"{account} 存储 {money} 成功。")
yuer = accounts[account]
return yuer, money

def get_money(self, account, money):
yuer = None
if accounts[account]:
if accounts[account] >= money:
accounts[account] -= money
print(f"{account} 取出 {money} 成功。")
yuer = accounts[account]
return yuer, money

atm = ATM()

acc = input("Enter account:")
print("1:存钱;2:取钱;3:查询")
x = int(input("Enter"))

if(x == 2):
try:
yuer, money = atm.get_money(acc, 10000)
print(f"余额:{yuer} 取出:{money} Happy去")
except KeyError as e:
print("账号不存在")
print(e)
if(x == 1):
try:
yuer, money = atm.set_money(acc, 10000)
print(f"余额:{yuer} 存入:{money} ")
except KeyError as e:
print("账号不存在")
print(e)

if(x == 3):
try:
yuer= atm.query_money(acc)
print(f"余额:{yuer}")
except KeyError as e:
print("账号不存在")
print(e)

"""
后面自己去补充
"""