1. class Person: def __init__(self,sex=None): self.sex=sex def eat(self): print("正在吃饭...") def sleep(self): print("正在睡觉...") def say(self): print("我的姓名:%s,我的年龄:%d"%(self.name,self.age)) p=Person() p.eat() p.sleep() #添加对象属性,只属于当前对象 p.name='zhangsan' p.age=21 #print("姓名:%s,年龄:%d"%(p.name,p.age)) p.say() lisi=Person() lisi.name="lisi" lisi.age=11 lisi.say() 2.
class Student(object): name=None age=None sex=None def __init__(self,name=None,age=None,sex=None): print("init...") self.name=name self.age=age self.sex=sex def study(self): print("{0}在学习".format(self.name)) def __siyou(self): print("siyou") def __str__(self): self.__siyou() return "姓名:{0},年龄:{1},性别:{2}".format(self.name,self.age,self.sex) def __del__(self): print("对象正在销毁...") s=Student('张三') del s input()