class Car():
"""A simple attempt to represent a car."""
def __init__(self, make, model, year):
self.make = make
self.model = model
self.year = year
self.odometer_reading = 0
def get_descriptive_name(self):
long_name = str(self.year) + " " + self.make + " " + self.model
return long_name.title()
class ElectricCar(Car):
"""Represent aspects of a car, specific to electric vehicles."""
def __init__(self, make, model, year):
"""Initialize attributes of the parrent class."""
super().__init__(make, model, year)
my_tesla = ElectricCar('tesla', 'model s', 2016)
print(my_tesla.get_descriptive_name())
如何从class ElectricCar(Car)
中删除属性year
?
我希望属性year
仍在父类class Car()
中,但要从子类中删除
是的,你可以,但这样做是不对的。如果
year
不应在ElectricCar
中使用,则它必须位于另一个类或对象中的某个位置。在父级中放置某些内容,然后在子级中删除它是不对的。但是顺便说一下,你可以这样做:相关问题 更多 >
编程相关推荐