I finally had to use “Get the Code” because I clearly don’t understand how this is supposed to work.
In Introduction to Classes 13/18, we created the PartTimeEmployee class which inherits from Employee without having the “name” member variable redefined in PartTimeEmployee.
Using this as an example, I attempted the same approach for the ElectricCar in Classes 9/11 by only defining a new member variable for the battery. This unconditionally failed. After days of retries, I finally had CodeAcademy give me the code and saw that, as far as I can tell, there’s no point to bothering with inheritance (at least in this scenario). The only thing that ElectricCar takes from Car is the “condition”. We saved ourselves and entire ONE line of code. Yippee?
Can anyone help me see the connection I’m missing? Why were we able to inherit a member variable in one lesson, but not in another?
Introduction to Classes 13/18
class Employee(object): """Models real-life employees!""" def __init__(self, employee_name): self.employee_name = employee_name def calculate_wage(self, hours): self.hours = hours return hours * 20.00 class PartTimeEmployee(Employee): def calculate_wage(self, hours): self.hours = hours return hours * 12 def full_time_wage(self, hours): return super(PartTimeEmployee, self).calculate_wage(hours) milton = PartTimeEmployee("Milton") print milton.full_time_wage(10)
class Car(object): condition = "new" def __init__(self, model, color, mpg): self.model = model self.color = color self.mpg = mpg def display_car(self): print "This is a %s %s with %s MPG." % (self.color, self.model, str(self.mpg)) def drive_car(self): self.condition = "used" class ElectricCar(Car): def __init__(self, model, color, mpg, battery_type): self.model = model self.color = color self.mpg = mpg self.battery_type = battery_type my_car = ElectricCar("DeLorean", "silver", 88, "molten salt")