Python实验六 面向对象编程 继承
本文最后更新于 546 天前,其中的信息可能已经有所发展或是发生改变。

第1关:单继承

任务描述

本关任务:补全程序。 Shape类是Rectangle类的父类,Rectangle类是Square类的父类。

相关知识

为了完成本关任务,你需要掌握:1.单继承

编程要求

根据提示,在右侧编辑器补充代码。根据主程序,补全类的定义。

测试说明

平台会对你编写的代码进行测试:

测试输入:; 预期输出: Shape(color=orange) orange Shape(color=orange) Rect(Shape(color=orange),length=1.2,width=3.4) 4.08 Square(Rect(Shape(color=orange),length=5.6,width=5.6)) 31.360 Square(Rect(Shape(color=orange),length=5.6,width=5.6))

# import math
class Shape:
    def __init__(self,color='red'):
        self.color = color
    def __str__(self):
        return 'Shape(color='+self.color+')'
    
class Rectangle(Shape): # 矩形类为Shape类的子类
    def __init__(self,len,wid,color='red'):
        super().__init__(color)
        self.len = len
        self.wid = wid
    def __str__(self):
        return 'Rect(Shape(color='+self.color+'),length='+str(self.len)+',width='+str(self.wid) + ')'
    def get_area(self):
        return self.len * self.wid

class Square(Rectangle): # Square类为Rectangle类的子类
    def __init__(self,r,color='red'):
        super().__init__(r,r,color)
    def __str__(self):
        return 'Square(Rect(Shape(color='+self.color+'),length='+str(self.len)+',width='+str(self.wid) + '))'
    def get_area(self):
        # return math.pi * (self.len ** 2)
        return self.len ** 2


if __name__ == '__main__':
    s1 = Shape('orange')
    print(s1) # Shape(color=orange)
    print(s1.color)
    print(str(s1))

    r1 = Rectangle(1.2,3.4,'orange')
    print(r1) # Rect(Shape(color=orange),length=1.2,width=3.4)
    print(r1.get_area()) # 4.08

    sq1 = Square(5.6,'orange')
    print(sq1) # Square(Rectangle(Shape(color=orange),length=5.6,width=5.6))
    # print(sq1.get_area()) # 31.36
    print('{:.3f}'.format(sq1.get_area()))
    print(str(sq1))
    pass

第2关:菱形继承

任务描述

本关任务:补全程序。 B,C 类是A类的子类,D类是B类、C类的子类。

根据输出结果,补全程序。

相关知识

为了完成本关任务,你需要掌握:1. 菱形继承。

编程要求

根据提示,在右侧编辑器补充代码,

测试说明

平台会对你编写的代码进行测试:

测试输入:; 预期输出: in Class A in Class C in Class B in Class D

A
|------|
| |
B C
| |
|------|
D
class A:
    def m(self):
        print('in Class A')
    pass

#B,C 类是A类的子类,D类是B类、C类的子类。

class B(A): # B类是A类的子类
    def mB(self):
        print('in Class B')
    pass

class C(A): # C类是B类的子类
    def mC(self):
        super().m()
        print('in Class C')
    pass

class D(B,C): # D类的父类是B类和C类
    def m(self):
        self.mC()
        self.mB()
        print('in Class D')
    pass

if __name__ =='__main__':
    x = D()
    x.m()
    #ACBD

第3关:函数参数

任务描述

本关任务:补全程序。 根据输出结果补全程序。

相关知识

为了完成本关任务,你需要掌握:1. 变长非关键字参数;

编程要求

根据提示,在右侧编辑器补充代码,

测试说明

平台会对你编写的代码进行测试:

测试输入:; 预期输出:

8
17
<class 'dict'>
name sita
age 22
phone 1234567890
<class 'dict'>
name john
email john@mail.com
country Wakanda
age 25
phone 9876543210
一种思想:一个巨长继承的程序中,传一个字典进去,层层传递,各层继承各取所需,自己初始化自己的,方便的很。
def adder(*num):
    sum = 0
    for i in num:
        sum += i
    return sum

def intro(**data):
    print(type(data))
    for key,value in data.items():
        print(key,value)
        

if __name__ == '__main__':
    # 不改动如下代码
    print(adder(3,5))#8
    print(adder(1,2,3,5,6))#17

    intro(name='sita',age=22,phone=1234567890)
    intro(name='john',email='john@mail.com',country='Wakanda',age=25,phone=9876543210)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇