标签:面向对象 object bsp tools 特殊 ext ini person 代码
1 # class Person: 2 # def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来 3 # tools.run() 4 # print("很开心, 我能玩儿游戏了") 5 # 6 # 7 # class Computer: 8 # def run(self): 9 # print("电脑开机. 可以运行") 10 # 11 # class Phone: 12 # def run(self): 13 # print("手机开机, 可以运行") 14 # 15 # 16 # c = Computer() 17 # phone = Phone() 18 # 19 # p = Person() 20 # p.play(phone) 21 22 23 24 # 写一个植物大战僵尸 25 # 1. 植物 26 # 打僵尸. 僵尸掉血 27 # 2. 僵尸 28 # 吃植物. 植物掉血 29 30 class Plant: 31 def __init__(self, name, hp, ad): # 200 32 self.name = name 33 self.hp = hp 34 self.ad = ad 35 36 def attack(self, js): 37 print("植物攻击僵尸") 38 js.hp -= self.ad 39 print(f"僵尸掉血{self.ad}, 还剩下{js.hp}") 40 41 class JiangShi: 42 def __init__(self, name, hp, ad): # 1000 800 43 self.name = name 44 self.hp = hp 45 self.ad = ad 46 47 def attack(self, zw): 48 print("僵尸咬植物") 49 zw.hp -= self.ad 50 print(f"植物掉血{self.ad}, 还剩{zw.hp}") 51 52 53 # 植物 54 wd = Plant("歪脖子豌豆", 10, 20) 55 # 僵尸 56 js = JiangShi("铁桶僵尸", 200, 1) 57 wd.attack(js) 58 wd.attack(js) 59 wd.attack(js) 60 wd.attack(js) 61 wd.attack(js) 62 63 js.attack(wd) 64 js.attack(wd) 65 js.attack(wd) 66 js.attack(wd)
# class Boy: # # def __init__(self, name, girlFriend=None): # # 在初始化的时候可以给一个对象的属性设置成另一个类的对象 # self.girlFriend = girlFriend # 一个男孩有一个女朋友 # # def chi(self): # if self.girlFriend: # print(f"带着他的女朋友{self.girlFriend.name}去吃饭") # else: # print("单身狗, 吃什么吃? 滚去学习.") # # def movie(self): # if self.girlFriend: # print(f"带着他的女朋友{self.girlFriend.name}去看电影") # else: # print("单身狗, 看什么看? 滚去学习.") # # # class Girl: # def __init__(self, name): # self.name = name # # b = Boy("宝浪") # g = Girl("孙艺珍") # b.chi() # # # alex给包浪介绍了一个女朋. 孙艺珍 # b.girlFriend = g # b.chi() # # g2 = Girl("梁咏琪") # b.girlFriend = g2 # 换了个女朋友 # b.chi() class School: def __init__(self, name): self.teach_list = [] # 这里要装多个老师 self.name = name def zhaopin(self, teach): self.teach_list.append(teach) def shangke(self): for t in self.teach_list: t.work() class Teacher: def __init__(self, name): self.name = name def work(self): print(f"{self.name}在上课") lnh = School("老男孩") t1 = Teacher("武sir") t2 = Teacher("太白") t3 = Teacher("哪吒") t4 = Teacher("女神") t5 = Teacher("日天") t6 = Teacher("宝浪") lnh.zhaopin(t1) lnh.zhaopin(t2) lnh.zhaopin(t3)
# class Base: # def chi(self): # print("我会吃") # # # # 派生类 => 子类 # class Foo(Base): # 这个类继承了Base类. Foo类是对Base的一个扩展 # def he(self): # print("我会喝") # # f = Foo() # f.chi() # f.he() # # # class Cat: # 父类 => 基类 => 超类 # def catch_mouse(self): # print("猫可以抓老鼠") # # class BosiCat(Cat): # 子类 => 派生类 # pass # class Foo: # pass # # # print(hash(Foo)) # 可哈希 # print(hash(Foo())) # 我们写好的类和创建的对象默认都是可哈希的 # 去掉可哈希 # class Foo: # __hash__ = None # 当前类的对象不可哈希 # print(hash(Foo)) # 可哈希 # print(hash(Foo())) # TypeError: unhashable type: ‘Foo‘ # # class Foo: # def chi(self, food): # print("我爱吃鱼和", food) # # class Bar: # def chi(self, food): # print("我爱吃肉和", food) # # dic = {Foo: "鸡蛋", Bar: "香肠"} # # for k, v in dic.items(): # k().chi(v) # 类名 => 变量名 -> 为了今天作业 # def func(): # pass # # an = func # # an() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # class Foo(Base): # pass # obj = Foo(123) # obj.func1() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # # class Foo(Base): # def func1(self): # print("Foo. func1", self.num) # # obj = Foo(123) # obj.func1() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # self.func2() # def func2(self): # print("Base.func2") # # class Foo(Base): # def func2(self): # print("Foo.func2") # obj = Foo(123) # obj.func1() # class Base: # def __init__(self, num): # self.num = num # # def func1(self): # print(self.num) # self.func2() # # def func2(self): # print(111, self.num) # # class Foo(Base): # def func2(self): # print(222, self.num) # # lst = [Base(1), Base(2), Foo(3)] # for obj in lst: # obj.func2() # class Base: # def __init__(self, num): # self.num = num # def func1(self): # print(self.num) # self.func2() # def func2(self): # print(111, self.num) # # class Foo(Base): # def func2(self): # print(222, self.num) # # lst = [Base(1), Base(2), Foo(3)] # for obj in lst: # obj.func1()
lnh.zhaopin(t4)
lnh.zhaopin(t5)
lnh.zhaopin(t6)
lnh.shangke()
# # class Foo: # def __init__(self): # 初始化操作 # print("我是init, 我是老二") # print("初始化操作. 在创建对象的时候自动调用这个方法") # # def __new__(cls, *args, **kwargs): # 创建, 它是真正的构造方法, 可以开辟内存 # print("我是new. 我是老大") # return object.__new__(cls) # # # # 为了 对象() # def __call__(self, *args, **kwargs): # print("我是对象()") # # # 对象[] # def __getitem__(self, item): # print("item=",item) # print("你执行了__getitem__") # return "哈哈" # # # 对象[key] = value # def __setitem__(self, key, value): # print("key, ", key) # print("value, ", value) # # # del lst[1] # def __delitem__(self, key): # print("key=", key) # # # with 对象: # def __enter__(self): # print("我是enter") # # # with 对象: 代码执行完毕. 最后执行这里 # def __exit__(self, exc_type, exc_val, exc_tb): # print("我叫exit") # # def __len__(self): # print("我的天哪") # return 3 # # # f = Foo() # 自动执行__init__() # f() # 调用-> __call__() # print(callable(f)) # 对象() # print(f["李嘉诚"]) # 自动调用__getitem__() # f[‘jay‘] = "林俊杰" # del f[‘哈哈‘] # with f: # print("我是哈哈哈哈") # with open() : # lst = ["孙艺珍", "李金珠", "井柏然"] # # lst[2] # =>自动的调用__getitem__() # def func(): # pass # func = 3 # print(callable(func)) # 判断xxx是否是可调用的 # # f.__init__() # 第一次这么写. 以后别这么写 # lst = [1,2,3,4] # it = iter(lst) # # print(it.__next__()) # # print(next(it)) # __next__() # 面试之前翻一番 # 写出15个特殊成员, 并给出具体作用 # class H: # country = "大清" # # print(H.country) # 面向对象编程的执行流程 -> # 1. 加载类 -> 给类创建一个名称空间 -> 主要存放类变量. # 2. 创建对象 -> 先找类. -> 根据类来开辟内存 -> 执行类中的__new__() -> 执行__init__() -> 返回对象 class Student: def __init__(self, name, no, gender, cls, age): self.name = name self.no = no self.gender = gender self.cls = cls self.age = age # 这个对象字符串的表示. def __str__(self): # 返回该对象的字符串表示形式 return f"{self.name}, {self.no}, {self.gender}" def __repr__(self): # 该对象的官方的字符串表示形式 return f"{self.name}, {self.no}, {self.gender}" s = Student("董仲浩", "3", "男", "S18", "31") print(s)
标签:面向对象 object bsp tools 特殊 ext ini person 代码
原文地址:https://www.cnblogs.com/274831730wang/p/10145376.html