class C1: def fun(self): print('aaa') class C2(C1): def fun(self): super( ).fun() print('bbb') obj1 = C2() obj1.fun()
时间: 2024-02-06 16:04:46 浏览: 20
该程序的输出结果是:
```
aaa
bbb
```
在这个程序中,我们同样定义了一个名为 C1 的类,其中包含了一个名为 fun() 的方法,用于输出字符串 "aaa"。
接下来,我们定义了一个名为 C2 的类,它继承了 C1 类,并重写了 C1 类中的 fun() 方法。在 C2 类中,fun() 方法首先调用了 super().fun(),即调用父类 C1 中的 fun() 方法,输出字符串 "aaa";然后在输出字符串 "bbb"。
然后,我们创建了一个名为 obj1 的 C2 类的实例对象,并调用了它的 fun() 方法。由于 obj1 是 C2 类的实例对象,因此调用 obj1.fun() 方法时,会执行 C2 类中的 fun() 方法。在 C2 类的 fun() 方法中,我们调用了 super().fun(),即调用父类 C1 中的 fun() 方法,输出字符串 "aaa";然后在输出字符串 "bbb"。
因此,该程序的输出结果是 "aaa" 和 "bbb"。
相关问题
class C1: def fun(self): print('aaa') class C2(C1): def fun(self): super().fun() print('bbb') obj1=C2() obj1.fun()
这段代码创建了两个类C1和C2,其中C2继承自C1。C1中有一个名为“fun”的方法,它会打印字符串“aaa”。C2中也有一个名为“fun”的方法,它首先调用父类C1的“fun”方法,然后再打印字符串“bbb”。
接下来创建了一个C2的实例obj1,并调用了它的“fun”方法。由于C2继承了C1的“fun”方法,因此当obj1调用“fun”方法时,会先执行C1的“fun”方法打印“aaa”,然后再执行C2的“fun”方法打印“bbb”,最终输出结果为:
```
aaa
bbb
```
写出该程序的结果:class C1: def fun(self): print('aaa') class C2(C1): def fun(self): super( ).fun() print('bbb') obj1 = C2() obj1.fun()
该程序的输出结果为"aaa"和"bbb",因为该程序定义了两个类C1和C2,其中C2继承自C1。在C2的fun()方法中,通过super().fun()调用父类C1的fun()方法,输出"aaa"。接着输出"bbb"。在主程序中,创建了一个C2类的对象obj1,并调用了其fun()方法。由于C2类重写了父类C1的fun()方法,因此调用obj1.fun()时,会执行C2的fun()方法,输出"aaa"和"bbb"。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)