对象:某个具体事物的存在,在现实世界中看得见摸得着的,可直接使用的
类:具有共同特征和行为的一组对象的抽象定义。
#父类 class people(): extinct = True #类属性,如果需要在类外修改类属性,必须通过类对象去引用然后进行修改。 如果通过实例对象去引用,会产生一个同名的实例属性,这种方式修改的是实例 属性,不会影响到类属性,并且之后如果通过实例对象去引用该名称的属性,实 例属性会强制屏蔽掉类属性,即引用的是实例属性,除非删除了该实例属性。 __weight = 0 #私有类属性,不能够被子类所继承 #构造函数 def __init__(self,n,a): self.name = n #实例属性 self.age = a # 类方法,对类属性进行封装 @classmethod def get__weight(cls): return cls.__weight @classmethod def set__weight(cls, w): cls.__weight = w #重载,print时会调用该方法 def __str__(self): return '%s的年龄是%d,体重是%dkg'%(self.name,self.age,self.__weight) def fun(self): print("调用父类people的方法") #静态方法 @staticmethod def isalive(): if people.extinct: print("还活着") else: print("已死亡") #子类 class student(people): def __init__(self,n,a,w,g): people.__init__(self,n,a) self.__weight = w self.grade = g def __str__(self): return '%s的年龄是%d,体重是%dkg,分数是%d'%(self.name,self.age,self.__weight,self.grade) # 覆盖父类方法 def fun(self): print("调用子类student的方法") p = people('will',13) people.set__weight(33) print(p) print(people.get__weight()) people.isalive() s = student('sue',12,35,90) print(s) s.fun() #调用已被覆盖的父类方法 super(student,s).fun()