-1

I need to have the sub classes take the information from the superclass Vehicle and go into their appropriate classes and carry the information. However I cant get them to carry over into the newer classes. What do I need to do to allow inheritance of specific traits?

class Vehicle:
    def __init__(self, make , model):
        self.year = 2000
        self.make = make
        self.model = model
# a vehicle is instantiated with a make and model
    @property
    def year(self):
        return self._year

    print "year"
#mutator
    @year.setter
    def year(self, value):
        if (value >= 2000):
            self._year = value
        if (value <= 2018):
            self._year = value
        if (value > 2018):
            self.value = 2018
        else:
            self._year = 2000
    pass

class Truck(Vehicle):
    pass

class Car(Vehicle):
    pass

class DodgeRam(Truck):
    pass

class HondaCivic(Car):
    pass

ram = DodgeRam(2016)
print ram

civic1 = HondaCivic(2007)
print civic1

civic2 = HondaCivic(1999)
print civic2
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
RawStanky
  • 3
  • 6

1 Answers1

1

Based on your comment, "All I really need to know is how to inherit something from another class"

class Vehicle():
    def __init__(self):
        self.year = 2000

class Truck(Vehicle):
    pass

class DodgeRam(Truck):
    pass

print(DodgeRam().year)

This worked fine for me. The Inheritance of year was automatic and 2000 was printed by both Python2 and Python3.

Your problem would seem to be that you are feeding year inputs in at the individual car creation level, but are trying to store that value at the super level. You should store values at the lowest level they are shared:

class Vehicle():
    pass

class Car(Vehicle):
    def __init__(self):
        self.wheels = 4

class HondaCivic(year):
    def __init__(self, year):
        self.make = 'Honda'
        self.model = 'Civic'
        self.year = year

If you want to FORCE subclasses to have certain values (make/model) and not leave them blank, you can see Enforcing Class Variables in a Subclass

Lost
  • 998
  • 10
  • 17