# 1.定义一个类
class Person:
pass
# 2.根据类创建一个对向
p = Person()
# 3.给p对向,增加一些属性
p.age = 18 #增
print(id(p.age))
# p.hight = 180
p.age = 123 #改
print(id(p.age))
# # 4.验证是否添加成功
# print(p.age)
# # print(p.__dict__) #查
# print(p.sex) # 'Person' object has no attribute 'sex'
p.pets = ['xiaohua', 'xiaohei']
print(id(p.pets))
p.pets.append("xiaohong")
print(id(p.pets))
p.pets = ['1', '2']
print(id(p.pets))
python核心:面向对象-类的定义
于 2023-07-21 17:01:33 首次发布