Ejemplo n.º 1
0
def inheritance_two():
    car = {}

    driver = Passenger()
    driver.setName('Bob')
    driver.setAge(30)
    driver.setSeatPosition(0)
    driver.setIsDriver(True)

    passenger = Passenger()
    passenger.setName('Jim')
    passenger.setAge(40)
    passenger.setSeatPosition(1)

    person = Person()
    person.setName('Jack')
    person.setAge(25)

    car['driver'] = driver
    car['passenger'] = passenger
    car['otherpassenger'] = person

    for key in car:
        occupant = car[key]
        try:
            print('Occupant ' + occupant.getName() + (', driving' if occupant.getIsDriver() else ', passenger' ) )
        except AttributeError as ae:
            if isinstance(occupant, Human):
                print('Occupant ' + occupant.getName() )
            else:
                print( 'Error : ' + ae.message)
Ejemplo n.º 2
0
def inheritance_one():
    car = {}

    driver = Passenger()
    driver.setName('Bob')
    driver.setAge(30)
    driver.setSeatPosition(0)
    driver.setIsDriver(True)

    passenger = Passenger()
    passenger.setName('Jim')
    passenger.setAge(40)
    passenger.setSeatPosition(1)

    car['driver'] = driver
    car['passenger'] = passenger

    for key in car:
        occupant = car[key]
        print('Occupant ' + occupant.getName() + (', driving' if occupant.getIsDriver() else ', passenger' ) )
Ejemplo n.º 3
0
from Car import Car
from Passenger import Passenger
from Person import Person

# Create an instance of Car with the default empty occupant list
mycar = Car( make='Kia', model='Sportage')

# Create the driver and passenger, both instances of Passenger
driver = Passenger()
driver.setName('Paul')
driver.setAge(43)
driver.setIsDriver(True)
driver.setSeatPosition(0)

passenger = Passenger()
passenger.setName('Bob')
passenger.setAge(23)
passenger.setIsDriver(False)
passenger.setSeatPosition(1)

# Create a Person object
person = Person()
person.setName('Charlie')
person.setAge(35)

# Add both our passengers
mycar.addOccupant(driver)
mycar.addOccupant(passenger)

# And then our person. The addOccupant method does not specify an object type.
# We could add some handling inside the Car class to raise an error if