Python中的多态性和类

时间:2017-04-18 20:33:09

标签: python class polymorphism

enter image description here我无法完成学校的这项任务。这张图片是完成后的输出结果。

以下是我的任务:

“在main.py文件中创建了四个对象。(车辆,轿车,卡车,掀背车) main.py应该有一个main函数和一个显示每个对象内容的函数。它还应该使用loadCollection$函数来测试有效数据(网球鞋不是车辆!) 在vehicle.py文件中创建一个名为Automobile的超类和三个子类(轿车,卡车和掀背车) Automobile类应该有三个属性(make,engine_type和drive_type。图2显示了这些属性的值,如Basic Sedan,6个cylinder,Front Wheel。 Sedan子类应继承Automobile类并覆盖引擎类型。 Truck和hatchback子类应该继承Automobile类并覆盖引擎类型和驱动器类型。“

这是我的名为vehicles

的类文件
isinstance

这是我的主要文件,主要功能是

    #create a superclass named Automobile
class Automobile:

    #the __init__method accepts an argument for the automobiles make

    def __init__(self, make, engine_type, drive_type):
        self.__make = make
        self.__engine_type = engine_type
        self.__drive_type = drive_type

    def show_make(self):
        print('Automobile Make:', self.__make)

    def show_drive_type(self):
        print(self.__drive_type)

    def show_engine_type(self):
        print(self.__engine_type)



class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        print('4 cylinder')

    def drive_type(self):
        Automobile.__init__(self)


class Truck(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        print('8 cylinder')

    def drive_type(self):
        print('Four Wheel')


class Hatchback(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        print('Electric')

    def drive_type(self):
        print('All Wheel')

这是我得到的错误:    #import the vehicles file with classes import vehicles #define the main function def main(): #create an Automobile object, and all subclass objects automobile = vehicles.Automobile('Sedan', '6 Cylinder', 'Front Wheel') sedan = vehicles.Sedan() truck = vehicles.Truck() hatchback = vehicles.Hatchback() #display information about each one print('Here are some vehicles and their engine types.') print() show_auto_info(automobile) print() show_auto_info(sedan) print() show_auto_info(truck) print() show_auto_info(hatchback) print() show_auto_info('Tennis Shoes') #define the show_auto_info function def show_auto_info(car): if isinstance(car, vehicles.Automobile): car.show_make() car.show_drive_type() car.show_engine_type() else: print(car, 'is not a vehicle!') main()

使用退出代码1完成处理

3 个答案:

答案 0 :(得分:0)

问题是您的子类与父类没有相同的签名,因此考虑Automobile类是这样的:

class Automobile:

    #the __init__method accepts an argument for the automobiles make

    def __init__(self, make, engine_type, drive_type):
        self.__make = make
        self.__engine_type = engine_type
        self.__drive_type = drive_type

    # rest of the class

您的孩子课程应该说Sedan应该是:

class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        super().__init__(make, engine_type, drive_type)

但是,如果您想使此代码与Python 2和3兼容,您可以使用:

class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

如果您想了解有关在Python中扩展类的更多信息,可以查看this

<强>更新

创建子类的对象时,请使用此

sedan = vehicles.Sedan('make_value', 'engine_type_value', 'drive_type_value')

答案 1 :(得分:0)

Okey ......你在这里有点迷失......你的代码破碎了,但它会再次被打破,所以我会提前一步:

#create a superclass named Automobile
class Automobile:

    #the __init__method accepts an argument for the automobiles make

    def __init__(self, make, engine_type, drive_type):
        self.__make = make
        self.__engine_type = engine_type
        self.__drive_type = drive_type

    def show_make(self):
        print('Automobile Make:', self.__make)

    def show_drive_type(self):
        print(self.__drive_type)

    def show_engine_type(self):
        print(self.__engine_type)



class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        self.__engine_type = '4 cylinder'
        return self.__engine_type

    def drive_type(self):
        return self.drive_type


class Truck(Automobile):

    def __init__(self):
        Automobile.__init__(self, 'Truck', None, None)

    def engine_type(self):
        self.__engine_type = '8 cylinder'
        return self.__engine_type

    def drive_type(self):
        self.__drive_type = 'Four Wheel'
        return self.__drive_type


class Hatchback(Automobile):
... same here 

请注意,我在方法中添加了一些返回信息,因为后者必须执行一些额外的方法来打印汽车描述。

答案 2 :(得分:0)

这就是主要方法应该是这样的,你必须自己结束剩下的代码,如果你不理解我的代码,再写信给我,我会向你解释

主类:

#import the vehicles file with classes
import vehicles

#define the main function
def main():
    #create an Automobile object, and all subclass objects
    automobile = vehicles.Automobile('Sedan', '6 Cylinder', 'Front Wheel')
    sedan = vehicles.Sedan('Sedan', '24 Cylinder', 'amaizing car')
    truck = vehicles.Truck()



    #display information about each one
    print('Here are some vehicles and their engine types.')
    print()
    show_auto_info(automobile)
    print()
    show_auto_info(sedan)
    print()
    show_auto_info(truck)
    show_auto_info('Tennis Shoes')

#define the show_auto_info function
def show_auto_info(car):
    if isinstance(car, vehicles.Automobile):
        car.show_make()
        car.show_drive_type()
        car.show_engine_type()
    else:
        print(car, 'is not a vehicle!')



main()