#先定义类 #__init__方法用来为对象定制对象自己独有的特征 class LuffyStudent: school = "Luffycity" # 数据属性 # stu1,"王大拿","女",18 def __init__(self,name,sex,age): self.Name = name self.Sex = sex self.Age = age #stu1.Name = "王大拿" #stu1.Sex = "女" #stu.Age = 18 def learn(self): # 函数属性 print("is Learning") def eat(self): # 函数属性 print("is eating") def sleep(self): print("is sleeping") #后产生对象 stu1 = LuffyStudent("王大拿","女",18) # LuffStudent.__init__(stu1,"王大拿","女",18)
#加上__init__方法后,实例化的步骤
#1、先产生一个空对象stu1
#2、会触发 LuffStudent.__init__(stu1,"王大拿","女",18)
产生第二个对象
#增加第二个对象 stu2 = LuffyStudent("五六七","男",26) # 先产生空对象stu2,然后自动调用LuffyStudent.__init__方法 # 并把自身以及定义的三个属性当作参数传给__init__ # 即LuffStudent.__init__(stu2,"五六七","男",26)
其他方法
一、查
print(stu1.__dict__) # 查stu1的名称空间 print(stu1.Name)
二、改
#改 stu1.Name = "李二丫" print(stu1.Name)
三、删
#删 del stu1.Name print(stu1.__dict__)
四、增
#增 stu1.class_name = "Python" print(stu1.class_name)