首页 > 软件开发 > PYTHON >

如何在PYTHON里同时运用两个class

来源:互联网 2023-03-16 19:16:40 403

如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

工具/原料

  • python3

方法/步骤

  • 1

    #比如我们现在有一个案例如下:DSL办公区 - 实用经验教程分享!

    #有一辆货车,有车牌号,总体积,货物列表。DSL办公区 - 实用经验教程分享!

    #然后每个货物有自己的名字和体积。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 2

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self):DSL办公区 - 实用经验教程分享!

    passDSL办公区 - 实用经验教程分享!

    #首先我们需要创建一个货物类,接着开始初始化这个货物类。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 2该信息非法爬取自百度经验
  • 3

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self, name, volume):DSL办公区 - 实用经验教程分享!

    self.name = nameDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    #我们设置两个参数,需要输入货物名称和体积。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 4

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self, name, volume):DSL办公区 - 实用经验教程分享!

    self.name = nameDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return "The volume of %s is %f." %(self.name, self.volume)DSL办公区 - 实用经验教程分享!

    #这里设置一下货物返回的字符串。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 5

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self, name, volume):DSL办公区 - 实用经验教程分享!

    self.name = nameDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return "The volume of %s is %.2f." %(self.name, self.volume)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    chair = Cargo("chair", 3)DSL办公区 - 实用经验教程分享!

    desk = Cargo("desk", 5)DSL办公区 - 实用经验教程分享!

    computer = Cargo("computer", 1)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    print(chair)DSL办公区 - 实用经验教程分享!

    print(desk)DSL办公区 - 实用经验教程分享!

    print(computer)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这里我们让对象运用这个货物类,并且进行打印。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 6

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    rest_volume = self.volumeDSL办公区 - 实用经验教程分享!

    cargo_list = []DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #初始化货车类,接着设置一下属性。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 7

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    rest_volume = self.volumeDSL办公区 - 实用经验教程分享!

    cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s. And the rest of the volume is %.2f. We have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number,DSL办公区 - 实用经验教程分享!

    rest_volume,DSL办公区 - 实用经验教程分享!

    cargo_list))DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这里设置一下这个类要返回的字符串。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 8

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    rest_volume = self.volumeDSL办公区 - 实用经验教程分享!

    cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s. And the rest of the volume is %.2f. We have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number,DSL办公区 - 实用经验教程分享!

    rest_volume,DSL办公区 - 实用经验教程分享!

    cargo_list))DSL办公区 - 实用经验教程分享!

    def add_cargo (self, cargo):DSL办公区 - 实用经验教程分享!

    print("Add the %s" �rgo)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这里设置一下方法,显示添加的货物。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 9

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    rest_volume = self.volumeDSL办公区 - 实用经验教程分享!

    cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s. And the rest of the volume is %.2f. We have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number, rest_volume, cargo_list))DSL办公区 - 实用经验教程分享!

    def add_cargo (self, cargo):DSL办公区 - 实用经验教程分享!

    print("Add the %s" �rgo)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    truck_AAA = Truck("AAA", 15)DSL办公区 - 实用经验教程分享!

    print(truck_AAA)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这里出错了,是因为我们必须在剩余的面积和货物列表那里加上self.,因为要调用对象。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 10

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    self.rest_volume = volumeDSL办公区 - 实用经验教程分享!

    self.cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s.\nAnd the rest of the volume is %.2f.\nWe have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number, self.rest_volume, self.cargo_list))DSL办公区 - 实用经验教程分享!

    def add_cargo (self, cargo):DSL办公区 - 实用经验教程分享!

    print("Add the %s" �rgo)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    truck_AAA = Truck("AAA", 15)DSL办公区 - 实用经验教程分享!

    print(truck_AAA)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这里设置火车对象,并且打印一下这个对象看看返回什么。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 11

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self, name, volume):DSL办公区 - 实用经验教程分享!

    self.name = nameDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return "The volume of %s is %.2f." %(self.name, self.volume)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    self.rest_volume = volumeDSL办公区 - 实用经验教程分享!

    self.cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s.\nAnd the rest of the volume is %.2f.\nWe have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number, self.rest_volume, self.cargo_list))DSL办公区 - 实用经验教程分享!

    def add_cargo (self, cargo):DSL办公区 - 实用经验教程分享!

    print("Add %s" �rgo)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    chair = Cargo("chair", 3)DSL办公区 - 实用经验教程分享!

    desk = Cargo("desk", 5)DSL办公区 - 实用经验教程分享!

    computer = Cargo("computer", 1)DSL办公区 - 实用经验教程分享!

    print(chair)DSL办公区 - 实用经验教程分享!

    print(desk)DSL办公区 - 实用经验教程分享!

    print(computer)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    truck_AAA = Truck("AAA", 15)DSL办公区 - 实用经验教程分享!

    print(truck_AAA)DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(desk)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这里我们把对象桌子的返回字符串引入到货车对象的方法里面,并且返回字符串。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 12

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self, name, volume):DSL办公区 - 实用经验教程分享!

    self.name = nameDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return "The volume of %s is %.2f." %(self.name, self.volume)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    self.rest_volume = volumeDSL办公区 - 实用经验教程分享!

    self.cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s.\nAnd the rest of the volume is %.2f.\nWe have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number, self.rest_volume, self.cargo_list))DSL办公区 - 实用经验教程分享!

    def add_cargo (self, cargo):DSL办公区 - 实用经验教程分享!

    print("Add %s" �rgo)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    chair = Cargo("chair", 3)DSL办公区 - 实用经验教程分享!

    desk = Cargo("desk", 5)DSL办公区 - 实用经验教程分享!

    computer = Cargo("computer", 1)DSL办公区 - 实用经验教程分享!

    print(chair)DSL办公区 - 实用经验教程分享!

    print(desk)DSL办公区 - 实用经验教程分享!

    print(computer)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    truck_AAA = Truck("AAA", 15)DSL办公区 - 实用经验教程分享!

    print(truck_AAA)DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(chair)DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(desk)DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(computer)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #全部定义进去。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 13

    class Cargo:DSL办公区 - 实用经验教程分享!

    def __init__ (self, name, volume):DSL办公区 - 实用经验教程分享!

    self.name = nameDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return "The volume of %s is %.2f." %(self.name, self.volume)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    class Truck:DSL办公区 - 实用经验教程分享!

    def __init__ (self, number, volume):DSL办公区 - 实用经验教程分享!

    self.number = numberDSL办公区 - 实用经验教程分享!

    self.volume = volumeDSL办公区 - 实用经验教程分享!

    self.rest_volume = volumeDSL办公区 - 实用经验教程分享!

    self.cargo_list = []DSL办公区 - 实用经验教程分享!

    def __str__ (self):DSL办公区 - 实用经验教程分享!

    return ("The licence of the truck is %s.\nAnd the rest of the volume is %.2f.\nWe have the %s in this truck."DSL办公区 - 实用经验教程分享!

    %(self.number, self.rest_volume, self.cargo_list))DSL办公区 - 实用经验教程分享!

    def add_cargo (self, cargo): DSL办公区 - 实用经验教程分享!

    self.cargo_list.append(cargo.name)DSL办公区 - 实用经验教程分享!

    print("Add %s" �rgo.name)DSL办公区 - 实用经验教程分享!

    print(self.cargo_list)DSL办公区 - 实用经验教程分享!

    self.rest_volume -= cargo.volumeDSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    chair = Cargo("chair", 3)DSL办公区 - 实用经验教程分享!

    desk = Cargo("desk", 5)DSL办公区 - 实用经验教程分享!

    computer = Cargo("computer", 1)DSL办公区 - 实用经验教程分享!

    print(chair)DSL办公区 - 实用经验教程分享!

    print(desk)DSL办公区 - 实用经验教程分享!

    print(computer)DSL办公区 - 实用经验教程分享!

    truck_AAA = Truck("AAA", 15)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(chair)DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(desk)DSL办公区 - 实用经验教程分享!

    truck_AAA.add_cargo(computer)DSL办公区 - 实用经验教程分享!

    print(truck_AAA)DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    DSL办公区 - 实用经验教程分享!

    #这个时候我们可以把货物添加入列表,并且让体积自动计算。在add_cargo方法里面务虽然参数是cargo,但是是可以指定输入对象的属性的,比如cargo.name和carogo.volume.。DSL办公区 - 实用经验教程分享!

    如何在PYTHON里同时运用两个classDSL办公区 - 实用经验教程分享!

  • 注意事项

    • 注意设置的打印内容

    以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!DSL办公区 - 实用经验教程分享!


    标签: python学习

    办公区 Copyright © 2016-2023 www.bgqu.net. Some Rights Reserved. 备案号:湘ICP备2020019561号统计代码