属性.py 469 B

1234567891011121314151617181920
  1. # -*- coding: UTF-8 -*-
  2. class Person():
  3. city = 'BeiJing' # 类的数据属性
  4. def __init__(self, name, age):
  5. self.name =name # 实例对象的数据属性
  6. self.age = age
  7. def run(self): # 类的函数属性
  8. print(f"{self.name} is talk")
  9. xdc = Person('xdc', 18)
  10. xiguatian = Person('xiaoguatian', 19)
  11. # 所有的实例都可以调用类的所有属性
  12. print(xdc.city)
  13. xdc.run()
  14. print(xiguatian.city)
  15. xiguatian.run()