2 回答

TA貢獻1786條經驗 獲得超11個贊
print(s.__dict__)
給出{'height': 5, 'length': 5}
所以,它具有這兩個屬性。
附加信息:- 為了清楚起見,您還可以help在實例上使用。對于例如
print(help(s)) That will give:
Help on Square in module __main__ object:
class Square(Rectangle)
| Square(length)
|
| Method resolution order:
| Square
| Rectangle
| builtins.object
|
| Methods defined here:
|
| __init__(self, length)
| Initialize self. See help(type(self)) for accurate signature.
|
| ----------------------------------------------------------------------
| Methods inherited from Rectangle:
|
| area(self)
|
| perimeter(self)
|
| ----------------------------------------------------------------------
| Data descriptors inherited from Rectangle:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)

TA貢獻1805條經驗 獲得超9個贊
正方形物體將具有高度和長度。
Square 的構造函數僅接受長度,但這并不意味著 Square 可以擁有的唯一屬性就是長度。當我們調用超類的構造函數時,它有兩個高度和長度輸入,兩者僅傳遞長度。在父構造函數中,高度和長度設置為 length,這導致 Square 具有這兩個屬性。
添加回答
舉報