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
参考:http://blog.csdn.net/wklken/article/details/6313265
posted @ 2015-12-09 22:53  奋斗的珞珞  阅读(169)  评论(0编辑  收藏  举报