PyThon3类的继承
代码:
class Person: def __init__(self, name, age, sex): self.name = name self.age = age self.sex = sex def desc(self): print("name:{},age:{},sex:{}".format(self.name,self.age,self.sex)) class Student(Person): def __init__(self, name,age,sex,school): self.name = name self.age = age self.sex = sex self.school = school def desc_stu(self): print("name:{},age:{},sex:{},school:{}".format(self.name,self.age,self.sex,self.school)) if __name__ == '__main__': stu = Student("张三","18","男","清华") stu.desc_stu() stu.desc(
运行结果:
name:张三,age:18,sex:男,school:清华
name:张三,age:18,sex:男