“Python案例:Person类的构造、继承和重写”的版本间的差异
来自CloudWiki
第61行: | 第61行: | ||
guy03.info()#子类实例调用重写父类的方法 | guy03.info()#子类实例调用重写父类的方法 | ||
+ | |||
+ | ==全部代码== | ||
+ | |||
+ | <nowiki> | ||
+ | class Person: | ||
+ | #构造函数 | ||
+ | def __init__(self,name,gender="female",age=19): | ||
+ | self.name=name | ||
+ | self.gender=gender | ||
+ | self.age=age | ||
+ | |||
+ | #析构函数 | ||
+ | def __del__(self): | ||
+ | print("销毁对象{0}".format(self)) | ||
+ | |||
+ | def show(self): | ||
+ | print(self.name,self.gender,self.age) | ||
+ | |||
+ | def info(self): | ||
+ | print(self.name) | ||
+ | |||
+ | class Student(Person):#Student类继承自Person类 | ||
+ | #构造函数 | ||
+ | def __init__(self,name,gender,age,major,dept): | ||
+ | Person.__init__(self,name,gender,age) | ||
+ | self.major =major | ||
+ | self.dept = dept | ||
+ | |||
+ | #子类Student重写父类的info方法 | ||
+ | def info(self): | ||
+ | print(self.name,self.major,self.dept) | ||
+ | |||
+ | if __name__ =="__main__": | ||
+ | guy01 = Person("张红","female",20)#构建实例时,自动调用构造函数 | ||
+ | guy02 = Person("王艳") | ||
+ | guy01.show() | ||
+ | guy02.show() | ||
+ | del guy01#删除实例时,自动调用析构函数 | ||
+ | del guy02 | ||
+ | #创建子类student的实例 | ||
+ | guy03 = Student("王明","male",10,"computer","cloud") | ||
+ | guy03.show()#子类实例可以调用父类的方法 | ||
+ | guy03.info()#子类实例调用自己重写父类的方法</nowiki> |
2022年12月20日 (二) 02:35的版本
Person类的构造与析构函数
class Person: #构造函数 def __init__(self,name,gender="female",age=19): self.name=name self.gender=gender self.age=age #析构函数 def __del__(self): print("销毁对象{0}".format(self)) def show(self): print(self.name,self.gender,self.age) def info(self): print(self.name) if __name__ =="__main__": guy01 = Person("张红","female",20)#构建实例时,自动调用构造函数 guy02 = Person("王艳") guy01.show() guy02.show() del guy01#删除实例时,自动调用析构函数 del guy02
Person类的继承
编写一个Student类,继承自Person类.
class Student(Person):#Student类继承自Person类 #构造函数 def __init__(self,name,gender,age,major,dept): Person.__init__(self,name,gender,age) self.major =major self.dept = dept
在main方法中创建子类Student类的实例:
guy03 = Student("王明","male",10,"computer","cloud") guy03.show()#子类实例可以调用父类的方法
Person类方法的重写
在子类Student中重写父类的方法info:
class Student(Person):#Student类继承自Person类 #构造函数 def __init__(self,name,gender,age,major,dept): Person.__init__(self,name,gender,age) self.major =major self.dept = dept #子类Student重写父类的info方法 def info(self): print(self.name,self.major,self.dept)
在main方法中子类实例调用重写父类的方法
guy03.info()#子类实例调用重写父类的方法
全部代码
class Person: #构造函数 def __init__(self,name,gender="female",age=19): self.name=name self.gender=gender self.age=age #析构函数 def __del__(self): print("销毁对象{0}".format(self)) def show(self): print(self.name,self.gender,self.age) def info(self): print(self.name) class Student(Person):#Student类继承自Person类 #构造函数 def __init__(self,name,gender,age,major,dept): Person.__init__(self,name,gender,age) self.major =major self.dept = dept #子类Student重写父类的info方法 def info(self): print(self.name,self.major,self.dept) if __name__ =="__main__": guy01 = Person("张红","female",20)#构建实例时,自动调用构造函数 guy02 = Person("王艳") guy01.show() guy02.show() del guy01#删除实例时,自动调用析构函数 del guy02 #创建子类student的实例 guy03 = Student("王明","male",10,"computer","cloud") guy03.show()#子类实例可以调用父类的方法 guy03.info()#子类实例调用自己重写父类的方法