Python就业班——初识面向对象——练习——People
Posted 嘆世殘者——華帥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python就业班——初识面向对象——练习——People相关的知识,希望对你有一定的参考价值。
多重继承_相同方法
1 #!/usr/bin/env python3 2 # coding=utf-8 3 # Version:python3.6.1 4 # Project:practice 5 # File:People.py 6 # Data:2020/7/12 12:04 7 # Author:LGSP_Harold 8 """ 9 已知People、Speaker、Student为三个自定义的类,其中People、S 10 peaker为Student的父类。请按照Python中多继承的方式进行编码实 11 现下列效果图所示功能。(注:People和Speaker中分别包含不同功 12 能的__init__( )和speak( )方法) 13 14 任务 15 1、自定义People类,并重写其构造(初始化)方法__init__( ),将参 16 数n和a赋值给实例对象的属性 17 2、自定义该类实例方法speak( ),功能:打印“xxx说: 我xxx岁” 18 3、自定义Speaker类,并重写其构造(初始化)方法__init__( ),将参 19 数n、c、t赋值给实例对象的属性 20 4、自定义该类实例方法speak( ),功能:打印“我叫xxx,我是一个xx 21 x,我演讲的主题是 xxx” 22 5、实例化Student类对象s 23 6、调用父类的speak( )方法 24 7、根据效果图进行格式化输出 25 """ 26 27 28 class People(object): 29 30 def __init__(self, name, age): 31 self.name = name 32 self.age = age 33 34 def speak(self): 35 print(‘My name is {0}, my age is {1}.‘.format(self.name, self.age)) 36 37 38 class Speaker(object): 39 40 def __init__(self, name, job, language): 41 self.name = name 42 self.job = job 43 self.language = language 44 45 def speak(self): 46 print(‘my name is {0}, I am a {1}, The Theme of my speech today is {2}.‘.format(self.name, self.job, self.language)) 47 48 49 class Student(Speaker, People): 50 51 def __init__(self, name, age, job, language): 52 People.__init__(self, name, age) 53 Speaker.__init__(self, name, job, language) 54 55 def speak(self, classStudentSon, classSpeakerFather, classPeopleFather): 56 super(Student, self).speak() 57 s1 = issubclass(classStudentSon, classSpeakerFather) 58 s2 = issubclass(classStudentSon, classPeopleFather) 59 print(‘Student is or not is Speaker son: {0}; Student is or not is People son: {1}‘.format(s1, s2)) 60 61 62 if __name__ == ‘__main__‘: 63 s = Student(‘Harold‘, 18, ‘Student‘, ‘Python‘) 64 s.speak(Student, Speaker, People)
以上是关于Python就业班——初识面向对象——练习——People的主要内容,如果未能解决你的问题,请参考以下文章