python:类
#coding=utf-8 #基类 class base: def __init__(self,name,age,weight): self.name=name#公有属性 self.age=age self.__weight=weight#私有属性 def speak(self):#公有方法 self.__run() print ("%s is speaking: I am %d years old" %(self.name,self.age)) def __run(self):#私有方法 print 'I am running' #派生类 class people(base): def __init__(self,n,a,w,m): base.__init__(self, n, a, w) self.major=m #重写基类方法 def speak(self): print("%s is speaking: I am %d years old,and I am major in %s"%(self.name,self.age,self.major)) if __name__ == '__main__': sbase=base(u'小马哥',25,130,) sbase.speak() print sbase.age,sbase.name
作者:奋斗的珞珞
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.