Python 类

#coding:utf-8
class people():
  #基本属性
    name = "hostname"
    age =  2
  #私有属性
    __weight = 0
    def __init__(self,n,a,w):
        self.name = n
        self.age = a
        self.__weight = w 

    def aa(self):
        print "hello wold"
    def speak(self):
        print "%s is speaking I am %d years old"  %(self.name,self.age)
class student(people):
    def __init__(self,n,a,w,g):
        #继承类中的构建函数,参数
        people.__init__(self,n,a,w)
        self.grade = g 
        #可覆盖继承类中的方法
    def speak(self):     
            print "%s is speaking: I am %d years old,and I am in grade %d"%(self.name,self.age,self.grade)
a = student('tom',25,30,32)
a.speak()
a.aa()
原文地址:https://www.cnblogs.com/lliqiang/p/6367286.html