day23_python
2021/8/23 9:28:46
本文主要是介绍day23_python,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
参考Eva_J的博客,原文连接:https://www.cnblogs.com/Eva-J/p/7277026.html
命名空间
from math import pi class Course: language = 'Chinese ' def __init__(self, teacher, course, period, price) -> None: self.teacher = teacher self.name = course self.period = period self.price = price Course.language = 'English' # Course.__dict__['language']='Chinese' # 会报错,无法修改,只能查看 print(Course.language) python = Course('egon', 'math', '6 months', '20000') # 类中的静态对象,可以被类和对象调用,类对象指针 # 对于不可变的数据库类型来说,类变量最好用类名操作 # 对于可变数据类型来说,修改是共享的,重新赋值独立的 # 模拟人生
类中变量值的修改
class Person(): money = 0 def work(self): self.money += 1000 # Person.money +=1000 mother = Person() father = Person() mother.money += 1000 father.money += 1000 print(Person.money) # 上述例子表示mother和father的money都在自己的空间中,并未改变Person的值 # 修改 Person.money += 1000 Person.money += 1000 print(Person.money) mother.work() father.work()
每实例化一个对象就记录下来,最终所有的对象共享这个数据
class Foo: count = 0 def __init__(self) -> None: Foo.count += 1 f1 = Foo() f2 = Foo() print(f1.count) f3 = Foo() print(f1.count) # 对象调用方法会有绑定方法的概念 # 类里面没有的变量无法找到全局的变量
面向对象的三大特征:继承 多态 组合
组合
例1 装备武器
class Dog: def __init__(self, name, blood, aggr, kind): self.name = name self.blood = blood self.aggr = aggr self.kind = kind def bite(self, person): person.blood -= self.aggr class Person: def __init__(self, name, blood, aggr, sex): self.name = name self.blood = blood self.aggr = aggr self.sex = sex self.money = 0 def attack(self, dog): dog.blood -= self.aggr def get_weapon(self, weapon): if self.money >= weapon.price: self.money = weapon.price self.weapon = weapon self.aagr += weapon else: print('余额不足') class Weapon: def __init__(self, name, aggr, njd, price) -> None: self.name = name self.aggr = aggr self.njd = njd self.price = price def hand18(self, person): if self.njd > 0: person.hp -= self.aggr*2 self.njd -= 1 alex = Person('alex', 0.5, 100, 'None') jin = Dog('金老板', 100, 500, 'teddy') w = Weapon('打狗棒', 100, 3, 998) alex.money += 1000 alex.get_weapon(w) print(alex.weapon) print(alex.aggr) alex.attack(jin) print(jin.blood) alex.weapon.hand18(jin) print(jin.blood) # 装备的伤害值、血量 # 组合,在一个对象的属性值是另外一个类的对象 # alex.weapon 是类weapon的对象
例2 圆环周长和面积
class Circle: def __init__(self, r) -> None: self.r = r def area(self): return pi*(self.r**2) def perimeter(self): return 2*pi*self.r class Ring: def __init__(self, out_r, in_r) -> None: self.out_c = Circle(out_r) self.in_c = Circle(in_r) def area(self): return self.out_c.area()-self.in_c.area() def perimeter(self): return self.out_c.perimeter()+self.in_c.perimeter() ring = Ring(20, 10) print(ring.area()) print(ring.perimeter())
例3 老师生日
class Birthday: def __init__(self, year, month, day) -> None: self.year = year self.month = month self.day = day class Teacher: def __init__(self, name, age, sex, birthday) -> None: self.name = name self.age = age self.sex = sex self.birthday = birthday self.course = Course(self, 'python', '6 month', 2000) # 也是一种组合,不过不具备通用性 b = Birthday(2018, 1, 16) egg = Teacher('egon', 0, '女', b) print(egg.name) print(egg.birthday.year) print(egg.course.price)
这篇关于day23_python的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南
- 2024-12-20Python编程基础与进阶
- 2024-12-19Python基础编程教程
- 2024-12-19python 文件的后缀名是什么 怎么运行一个python文件?-icode9专业技术文章分享
- 2024-12-19使用python 把docx转为pdf文件有哪些方法?-icode9专业技术文章分享
- 2024-12-19python怎么更换换pip的源镜像?-icode9专业技术文章分享
- 2024-12-19Python资料:新手入门的全面指南
- 2024-12-19Python股票自动化交易实战入门教程
- 2024-12-19Python股票自动化交易入门教程