[英]Getting an AttributeError in Python code (while inheriting from Parent class) - Assignment 30 of Object Oriented Programming (INFYTQ)
我正在練習一些面向對象的分配,在這種情況下,我想繼承父類的private屬性。 我想在某些條件下進行檢查。 有什么辦法可以通過繼承提取私有屬性的值。 我使用了getter方法,但仍然出現錯誤。
class Pizzaservice:
counter = 100
def __init__(self, customer, pizza_type, additional_topping):
self.__service_id = None
self.__customer = customer
self.__pizza_type = pizza_type
self.__additional_topping = additional_topping #boolean (T=req)
self.pizza_cost = None
def validate_pizza_type(self):
if(self.get_pizza_type() == "Small" or self.get_pizza_type() == "Medium"):
return True
else:
return False
def calculate_pizza_cost(self):
a = self.validate_pizza_type()
b = self.__customer.validate_quantity()
if(a == True and b == True):
q = self.__customer.get_quantity()
if(self.__pizza_type == "Small"):
if(self.__additional_topping == True):
self.pizza_cost = (150 + 35) * q
else:
self.pizza_cost = 150 * q
elif(self.__pizza_type == "Medium"):
if(self.__additional_topping == True):
self.pizza_cost = (200 + 50) * q
else:
self.pizza_cost = 200 * q
Pizzaservice.counter += 1
self.__service_id = self.__pizza_type[0:1] + str(Pizzaservice.counter)
else:
self.pizza_cost = -1
def get_service_id(self):
return self.__service_id
def get_pizza_type(self):
return self.__pizza_type
def get_customer(self):
return self.__customer
def get_additional_topping(self):
return self.__additional_topping
class Doordelivery(Pizzaservice):
def __init__(self, customer, pizza_type, additional_topping, distance_in_kms):
self.__delivery_charge = None
self.__distance_in_kms = distance_in_kms
def validate_distance_in_kms(self):
if(self.__distance_in_kms >=1 and self.__distance_in_kms <=10):
return True
else:
return False
def calculate_pizza_cost(self):
d = self.validate_distance_in_kms()
if(d == True):
temp = super().calculate_pizza_cost()
if(temp != -1):
if(self.__distance_in_kms <= 5):
self.__delivery_charge = 5 * self.__distance_in_kms
self.pizza_cost = self.pizza_cost + self.__delivery_charge
else:
self.__delivery_charge = 25 + (7 * (self.__distance_in_kms - 5))
self.pizza_cost = self.pizza_cost + self.__delivery_charge
else:
self.pizza_cost = -1
def get_delivery_charge(self):
return self.__delivery_charge
def get_distance_in_kms(self):
return self.__distance_in_kms
class Customer:
def __init__(self, customer_name, quantity):
self.__customer_name = customer_name
self.__quantity = quantity
def validate_quantity(self):
if(self.__quantity >= 1 and self.__quantity <= 5):
return True
else:
return False
def get_customer_name(self):
return self.__customer_name
def get_quantity(self):
return self.__quantity
c = Customer("Sanya", 2)
obj_1 = Pizzaservice(c, "Small", True)
obj_2 = Doordelivery(c, "Small", True, 2)
obj_1.calculate_pizza_cost()
obj_2.calculate_pizza_cost()
我收到此錯誤:
追溯(最近一次通話):
第89行,在
obj_2.calculate_pizza_cost()
第56行,在calculate_pizza_cost中
臨時= super()。calculate_pizza_cost()
第15行,在calculate_pizza_cost中
一個= self.validate_pizza_type()
第10行,在validate_pizza_type中
if(self.get_pizza_type()==“ Small”或self.get_pizza_type()==“ Medium”):
第38行,在get_pizza_type中
返回self .__ pizza_type
AttributeError:'Doordelivery'對象沒有屬性'_Pizzaservice__pizza_type'
我不了解繼承部分嗎? 我應遵循什么程序來糾正此問題?
如果我沒看錯,由於您定義的是與父類不同的新構造函數,因此您將覆蓋__init__
函數。 這就是為什么找不到變量的原因,因為未調用父類構造函數! 您需要調用super().__init__(...)
來初始化您在父類中定義的變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.