一:命名空间
class Person: Country = '中国人' # 静态变量 print(Person.Country) alex = Person() # 创建了一个空的命名空间 alex.name = 'alex' # 对象 alex.Country = '泰国人' egon = Person() egon.name = 'egon' # 类名.静态变量 对象.属性名 # 类名可以调用对象的属性么? 不可以 # 对象可以调用类中的属性么? 可以 print(Person.Country) print(alex.Country) print(egon.Country)
打印结果
注意:类名不可以调用对象的属性(不能清楚具体是哪个对象属性,两个country)
对象名可以调用类中的属性(alex.country)
由于对象和类之间存在一个关联关系 所以对象能够找到类 但是 类不能找到对象
二:对象.属性命名空间寻找(先在自己命名空间找,后再类命名空间找)
#使用类名.属性 只会寻找类中的静态变量名字 #使用对象.属性 会先在对象自己的命名空间中找名字 # 如果找不到 再到类的内存空间中去找 class Person: Country = '中国人' # 静态变量 alex = Person() egon = Person() print(alex.Country) alex.Country = '印度人' print(alex.Country) Person.Country # 只要你使用静态变量 就用类名去调用
注意:当你使用静态变量时候,就用类名去调用
例子1
class Person: money = 0 mother = Person() father = Person() Person.money += 1000 Person.money += 1000 print(Person.money) print(mother.money) print(father.money)
打印结果:2000,2000,2000
例子2
class Person: money = [1] mother = Person() father = Person() mother.money[0] += 1000 [0]表示索引 father.money[0] += 1000 [0]表示索引 print(mother.money) print(father.money) print(Person.money)
打印结果 [2001],[2001],[2001]
注意:[0]表示索引,+= 1000 就是索引加。
例子3
class Person: money = [0] mother = Person() father = Person() mother.money = [1000] father.money = [2000] print(mother.money) print(father.money) print(Person.money)
打印结果:【1000】,【2000】,【0】 例子3中索引改变才让调用类属性改变,而本题没有变化。
例子4
a = 1 a = 2 print(a) a = [1] a.append(2) print(id(a)) a[0] = 10 print(id(a)) print(a) a = [1] a = [2]
打印结果:2,40615688,40615688,【10,2】
例子5:
写一个类,能统计这个类被多少个对象实例化了. 所有的对象共享这个结果 init 静态变量 class Foo: num = 0 def __init__(self): Foo.num += 1 f1 = Foo() print(Foo.num) f2 = Foo() print(Foo.num) print(f1.num)
组合:圆和老师组合例题
组合 两个类的事儿 什么叫组合 : 一个类对象的属性 是 另外一个类的对象 两个类的事儿 :类与类之间有一种"什么有什么的关系" 圆的类 圆环 和 圆 圆环 也是一个类 属性 大圆半径 和 小圆半径 圆环 求面积 求周长
from math import pi class Circle: def __init__(self,r): self.r=r def area(self): return pi*(self.r**2) class Ring: def __init__(self,outer,inner): self.outer=Circle(outer) self.inner=Circle(inner) def area(self): return self.outer.area()-self.inner.area() r=Ring(10,3) c=Circle(11) print(Ring.area(r)) print(r.area())
例题:老师生日组合
参考答案
老师 name sex course(课程) birth 生日 年月日 class Birthday: def __init__(self,year,month,day): self.year = year self.month = month self.day = day class Teacher: def __init__(self,name,sex,course,birth): self.name = name self.sex = sex self.course = course self.birth = birth # birth是一个对象 birth = Birthday(1960,3,7) # birth 是Birthday类的一个对象 alex = Teacher('alex','male','python',birth) # alex.birth = birth # time '1960-3-7' # 老师的年龄 2018 - 1960 print(birth) import time if birth.month == time.localtime().tm_mon and \ birth.day == time.localtime().tm_mday: print('生日快乐') print(time.localtime().tm_year - birth.year)
自己写:
class Birthday: def __init__(self,year,month,day): self.year=year self.month=month self.day=day class Teacher: def __init__(self,name,course,birth): self.name=name self.course=course self.birth=birth birth=Birthday(1960,4,4) zhen=Teacher('zhen','python',birth) # print(Birthday.year()) print(zhen.birth.year) print(birth.year)