Python就业班——初识面向对象——练习——People

2021-04-19 18:26

阅读:677

标签:student   Oday   elf   功能   自定义   方式   定义   继承   color   

多重继承_相同方法

 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

标签:student   Oday   elf   功能   自定义   方式   定义   继承   color   

原文地址:https://www.cnblogs.com/Harold-Hua/p/13288046.html


评论


亲,登录后才可以留言!