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
| """ 创建类和对象 """
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("已经创建一个学生对象,并且做了初始化")
def __str__(self): print("__str__(self)") return f"{self.name}{self.age}{self.address}"
def __lt__(self,other): print("__lt__(self,other)") return self.age < other.age
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")
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: def __new__(cls,*args,**kwargs): print("__new__(cls,*args,**kwargs)") print("new object") return super().__new__(cls)
def __init__(self,*args,**kwargs): print("__init__(self,*args,**kwargs)") print("init object") super().__init__(*args,**kwargs)
def __del__(self): print("del object")
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)
""" 后面自己去补充 """
|