我有个问题。在使用 python 的 oops 中, super 可以访问方法或构造函数,但不能访问属性。这是为什么??
class Phone:
def __init__(self, price, brand, camera):
print ("Inside phone constructor")
self.__price = price
self.brand = brand
self.camera = camera
def buy(self):
print ("Buying a phone")
def return_phone(self):
print ("Returning a phone")
class FeaturePhone(Phone):
pass
class SmartPhone(Phone):
def __init__(self, price, brand, camera, os, ram):
super().__init__(price, brand, camera)
self.os = os
self.ram = ram
print ("Inside smartphone constructor")
def buy(self):
print(super().camera) #Error
print ("Buying a smartphone")
s=SmartPhone(20000, "Samsung", 12, "Android", 2)
print(s.buy()) #error
print(s.brand) …Run Code Online (Sandbox Code Playgroud)