判断题
偏易
#18153
运行以下python代码,屏幕将输出“derived class”。 class BaseClass : def my_method ( self ) : pr…
运行以下python代码,屏幕将输出“derived class”。
答案与解析
正确答案:对
解析
定义基类BaseClass:
基类 BaseClass 定义了一个方法 my_method,该方法打印字符串 “base class”。
定义派生类DerivedClass:
派生类 DerivedClass 继承自 BaseClass。
派生类重写了基类中的 my_method 方法,该方法打印字符串 “derived class”。
创建派生类实例并调用方法:
创建 DerivedClass 的一个实例 derived_instance。
调用实例方法 my_method。
运行结果
当创建DerivedClass的实例并调用其my_method方法时,由于DerivedClass重写了基类BaseClass中的my_method方法,因此会执行派生类中的方法,并打印字符串"derived class"。