使用类、类属性、对象属性

来源:互联网 发布:js取元素位置 编辑:程序博客网 时间:2024/06/03 11:16
#coding:utf-8'''1.类属性 -- 定义在类后2.数据属性 -- 定义在__init__方法中3.使用类属性c统计当前的对象个数'''class Hum(object):    #类属性c、sing    c=0    sing="hello"    def __init__(self,name,age):        #定义数据属性 self.name、self.age        self.name=name        self.age =age        #使用类属性c,计算学生人数        Hum.c+=1    def speak(self):        print "HUM"class Stu(Hum):    def __init__(self,age,name,score):        self.score=score        #继承父类的两种方法        #Hum.__init__(self,name,age)        super(Stu,self).__init__(name,age)    def info(self):        print self.name,self.age,self.score        #调用父类的speak方法        self.speak()        print self.sing    if __name__ == '__main__':    s = Stu("sam",26,100)    s.info()    print s.c # 1    s2=Stu("kitty",25,90)    s.info()    print s.c # 2    #通过类调用类属性    print Hum.c,Hum.sing # 2 hello

0 0
原创粉丝点击