class Temperature:
def __init__(self, temperature):
"""Gets temperature from user"""
self.__temperature = temperature
def aboveFreezing(self):
"""Returns True if temperature above freezing point"""
raise NotImplementedError("Method aboveFreezing not implemented")
def convertToFahren(self):
"""Returns a new Temperature object converted to degrees Fahrenheit"""
raise NotImplementedError("Method convertToFahren not implemented")
def convertToCelsius(self):
"""Returns a new Temperature object converted to degrees Celsius"""
raise NotImplementedError("Method convertToCelsius not implemented")
def convertToKelvin(self):
"""Returns a new Temperature object converted to degrees Kelvin"""
raise NotImplementedError("Method convertToKelvin not implemented")
class Kelvin(Temperature):
def __init__(self, temperature):
Temperature.__init__(self,temperature)
def __str__(self):
"""Returns string of temperature"""
return self.__temperature + 'degrees Kelvin.'
def aboveFreezing(self):
"""Returns True if temperature above freezing point"""
if self.__temperature > 273.15:
return True
else:
return False
def convertToFahren(self):
"""Returns a copy Kevin to Fahren"""
return ((self.__temperature * 9/5) - 459.67)
def convertToCelsius(self):
"""Returns conversion from Kelvin to Celsius"""
return self.__temperature - 273.15
def convertToKelvin(self):
"""Returns copy of Kelvin"""
return self.__temperature
我尝试运行开尔文课,所以我尝试:
t1 = Kelvin(33)
print(t1)
但它告诉我:
return self.__temperature + 'degrees Kelvin.'
AttributeError: 'Kelvin' object has no attribute '_Kelvin__temperature'
我是班级的新手。我认为这是因为我的错误:
def __init__(self, temperature):
Temperature.__init__(self,temperature)
因此,如果这是问题,我如何让开尔文类具有温度值?我必须让Kelvin成为温度的子类。如果我没有这样做,我知道我可以这样做: 自我.__温度=温度
但是我如何将Temperature类的温度分配给Kelvin子类?