迹忆客 计算机编程题库

Python 经典面试题 基础部分一

以下代码的输出是什么? ```python class P: def __init__(self): self.__x=100 self.y=200 def print(self): print(self.__x, self.y) class C(P): def __init__(self): super().__init__() self.__x=300 self.y=400 d = C() d.print() ```
  • 300 400
  • 100 400
  • 100 200
  • 300 200
正确答案是:B
正确率:36%

解析:

在上面的代码中,x是在类P中声明的私有变量。因此,在继承类P的类C中,不能更改x的值。但是y不是私有变量,因此可以更改其值。

查看笔记

扫码一下
查看教程更方便