小白学Python 之创建类 三
# 创建类 class myClass(object): name="东皇太一"; Age=416466856; shen="真神"; def use(self): print ("这是一个洪荒小说的妖神={}".format(self.name)) item=myClass(); print(item.name) item.name="太素元清"; print(item.name) print(item.use()) class myTest(object): def __init__(self,name="taiyi",Age=0,shen=""): self.name=name; self.Age=Age; self.shen=shen; def use(self): print ("这是一个洪荒小说的妖神={}".format(self.name)) test=myTest(); print (test.name) test1=myTest(name="北京",Age=600,shen="didou"); print (test1.name) #比较2个实体是否相等 class testeq(object): def __init__(self,age): self.age=age; def __eq__(self, other): if self.age==other.age: return True; else: return False; a=testeq(5); b=testeq(6); c=testeq(5); print (a==b); print (a==c);
类的继承
#类的继承 class myTest(object): def __init__(self,name="taiyi",Age=0,shen=""): self.name=name; self.Age=Age; self.shen=shen; def use(self): print ("这是一个洪荒小说的妖神={}".format(self.name)) def change_name(self,name): self.name=name; def change_shen(self,shen): self.shen=shen; class person(myTest): def __init__(self,title,book,name,Age,shen): super(myTest, self).__init__() self.title=title; self.book=book; self.name=name; self.Age=Age; self.shen=shen; bm=person(title="标题",book="一流",name="xiaoming",Age=100,shen="大牛"); print(bm.name)