【发布时间】:2019-11-15 16:17:56
【问题描述】:
我有一个问题。在使用 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)
谁能解释一下??如果可以的话怎么做?
【问题讨论】:
标签: python-3.x oop super