第27636题 单选题
以下关于Python类继承的代码片段,相关说法正确的是?
class Animal:
    def __init__(self, name):
        self.name = name
    def speak(self):
        print(f"{self.name} makes a sound")

class Dog(Animal):
    def __init__(self, name):
        super().__init__(name)
    def speak(self):
        print(f"{self.name} barks")

a = Animal("Generic")
d = Dog("Buddy")
A

执行a.speak()会输出 Buddy makes a sound

B

执行d.speak()会输出 Buddy makes a sound

C

在Dog类的speak方法中添加super().speak(),执行d.speak()会先输出Buddy makes a sound再输出Buddy barks

D

Dog类无法访问Animal类的name属性

程序运行统计
暂无判题统计
提交0次 正确率0.00%
答案解析