类的命名空间&组合

时间:2024-01-04 20:58:38
类的命名空间
◆类中的静态变量可以被类调用也可以被对象调用
◆对不可变数据类型来说,类变量最好用类名操作
class Person:
money = 0
mother = Person()
father = Person()
Person.money += 1000
Person.money += 1000
print(Person.money)

◆对于可变数据类型来说,对象的修改是共享的,重新赋值是独立的

class Course:
langauage = 'Chinese'
def __init__(self,name,period,price,teacher):
self.name =name
self.period =period
self.price =price
self.teacher =teacher
def func(self):
pass
python = Course('python','six months',2000,'Wang')
print(python.__dict__)
print(python.langauage)
Course.langauage = 'English'
print(Course.langauage)# English
print(python.langauage)# English
python.langauage = '阿拉伯'
print(python.langauage)# 阿拉伯 没有修改类中的值而是在python这个对象的命名空间中增加了一项'langauage': '阿拉伯'
print(Course.langauage)# English
print(python.__dict__) # {'name': 'python', 'price': 2000, 'period': 'six months', 'langauage': '阿拉伯', 'teacher': 'Wang'}
class Foo:
count = 0
def __init__(self):
Foo.count += 1
f1 = Foo()
f2 = Foo()
print(f1.count)
print(f2.count)
f3 = Foo()
print(f3.count)

练习:创建一个对象,每实例化一个对象就计数。 最终所有对象都共享这个数据

绑定方法:
◆只有对象调用类内的方法的时候才有绑定方法的概念,
◆当对象调用类内的方法的时候就是把对象中的值传给这个方法的self,所以这个对象和这个方法就有了绑定关系
#普通函数
def func():pass
print(func) #<function func at 0x0000023DFAD0E488>
# 类
class Foo:
def func(self):
print('func')
f1 = Foo()
print(Foo.func) # <function Foo.func at 0x0000023DFAD0E950>
print(f1.func) #对象调用方法 ,把f1的值传给self f1和func发生了绑定
# <bound method Foo.func of <__main__.Foo object at 0x000001F5784E8780>>
print(f1) # <__main__.Foo object at 0x000001F5784E8780> 补充:导入包的过程,就相当于实例化一个对象,就会自动执行__init__.py文件
面向对象的三大特性 : 继承   多态   封装

组合 : 一个类的对象是另一个类对象的属性

如:alex.weapon 是 Weapon 类的对象  (alex.weapon 相当于 w)
# 狗
class Dog:
def __init__(self,name,aggr,blood,kind):
self.name = name
self.aggr = aggr
self.blood = blood
self.kind = kind
def bites(self,person):
person.blood -= self.aggr
# 人
class Person:
def __init__(self,name,aggr,blood,sex):
self.name = name
self.aggr = aggr
self.blood = blood
self.sex = sex
self.money = 0
def attack(self,dog):
dog.blood -= self.aggr def get_weapon(self,weapon):
# 首先判断玩家的money够不够买装备
if self.money >= weapon.price:
# 玩家买完武器之后money减少
self.money -= weapon.price
# 玩家有了武器
self.weapon = weapon
# 玩家的攻击力增加
self.aggr += weapon.aggr
else:
print('余额不足,请先充值')
# 武器装备
class Weapon:
def __init__(self,name,price,aggr,naijiudu):
self.name = name
self.price = price
self.aggr = aggr
self.naijiudu = naijiudu
# 大招
def hand18(self,person):
if self.naijiudu > 0 : # 如果还有耐久度
person.blood -= self.aggr * 2 # 被攻击的人的血就掉攻击者攻击力的两倍
self.naijiudu -= 1 # 耐久度减一 alex = Person('alex',0.5,100,'女')
jin = Dog('金老板',100,500,'泰迪')
w = Weapon('打狗棒',998,100,3)
# alex 装备打狗棒
alex.money += 1000
alex.get_weapon(w)
print(alex.aggr) # 100.5
print(alex.weapon) # <__main__.Weapon object at 0x000001C8C55886A0>
# alex 攻击 金老板
alex.attack(jin)
print(jin.blood) # 399.5
# alex使用大招攻击金老板
alex.weapon.hand18(jin)
print(jin.blood) # 199.5

人狗大战升级版

from math import pi
class Cricle:
def __init__(self,r):
self.r = r
def area(self):
return pi * self.r ** 2
def perimeter(self):
return 2 * pi * self.r class Ring:
def __init__(self,outside_r,inside_r):
self.outside_c = Cricle(outside_r) # Cricle(outside_r)是Cricle的对象,但是在这里是Ring类的属性
self.inside_c = Cricle(inside_r)
def area(self):
return self.outside_c.area() - self.inside_c.area()
def perimeter(self):
return self.outside_c.perimeter() + self.inside_c.perimeter()
ring = Ring(20,10)
print(ring.area())
print(ring.perimeter())

栗子1:运用组合计算圆环的面积和周长

class Teacher:
def __init__(self,name,age,sex,brithday):
self.name = name
self.age = age
self.sex = sex
self.brithday = brithday
self.course = Course('alex',2000,'six months')
class Course:
def __init__(self,name,price,month):
self.name = name
self.price = price
self.month = month class Birthday:
def __init__(self,year,month,day):
self.year = year
self.month = month
self.day = day b = Birthday(2018,7,25)
alex = Teacher('alex',1,'女',b)
print(alex.name)
print(alex.brithday.year)
print(alex.brithday.month)
print(alex.brithday.day)
print(alex.course.price)

栗子2:用组合创建一个老师类,有生日且生日是一个类