2017-04-06 152 views
1

我做了两个班,一个父母和一个孩子。我想知道的是,是否可以使用父类来初始化子类。这是我的代码:如何从超类的实例创建子类的实例?

class Pc: 

    def __init__(self, brand, processor, price): 
     self.brand = brand 
     self.processor = processor 
     self.price = price 

class Laptop(Pc): 
    def __init__(self,brand,processor,price, battery): 
     Pc.__init__(self,brand,processor,price) 
     self.battery = battery 

现在我可以初始化电脑类:

b = Laptop('brand','processor','price') 

但是,这似乎不是多大用处的我也和我的老师没有解释得很好。我想知道是否有可能做这样的事情:当我尝试这与我的代码,我得到一个错误

a = Pc("brand","processor","price") 
b = Laptop(a, "battery") 

。这甚至有可能吗?

+1

您可以定义'高清__init __(自我,个人电脑,电池):'然后从'pc'参数中复制'Pc'属性。 – Barmar

+1

错误在于您定义了'Laptop .__ init__'接收4个参数,并且只给了它两个..您需要重新定义函数以接受要传递它的参数 – Aaron

+0

您可以修改laptop init * argv并以某种方式工作,具体取决于你有多少个参数,或者为这个特定方法制定一个类方法作为替代构造函数 – Copperfield

回答

3

你可以这样做:

class Laptop(Pc): 
    def __init__(self, pc, battery): 
     Pc.__init__(self, pc.brand, pc.processor, pc.price) 
     self.battery = battery 

此初始化从你给构造函数的参数pc继承的属性。

如果你希望能够使用短或长的方法,你应该定义构造器采取关键字参数,然后使用if语句来调用Pc.__init__()适当地根据其使用形式。

0

你这样做在一定程度上的hackish方式:

import copy 
a = Pc("brand","processor","price") 
b = copy.deepcopy(a) 
b.__class__ = Laptop() 
b.battery = battery 
1

没有在Python内置的机制来达到你想要什么,所以你必须自己做,或找到一个第三方解决方案。

一个好方法。这是我可能需要将增加对超类方法是可以复制你的对象:

class Pc: 

    @classmethod 
    def clone(cls, target, *extra_args, **extra_kwargs): 
     return cls(
      target.brand, target.processor, target.price, *extra_args, 
      **extra_kwargs) 

    def __init__(self, brand, processor, price): 
     self.brand = brand 
     self.processor = processor 
     self.price = price 


class Laptop(Pc): 

    def __init__(self, brand, processor, price, battery): 
     super(Laptop, self).__init__(brand, processor, price) 
     self.battery = battery 


a = Pc("brand","processor","price") 
b = Laptop.clone(a, 'battery') 

print(b.battery) 

但你会发现你开始运行与初始化麻烦参数。我会建议保持所需的参数__init__()到最小,然后事后配置必要的属性:

a = Pc() 
a.brand = 'brand' 
# etc. 
1

还有,如果你想keepthe目前的行为该

  1. 几个选项,同时加入了新的,你可以使用* ARG,例如像

    class Laptop(Pc): 
        def __init__(self, *argv): 
         if len(argv) == 4: #the current one 
          brand, processor, price, battery = argv 
         elif len(argv) == 2: #the new one 
          pc, battery = argv 
          brand, processor, price = pc.brand, pc.processor, pc.price 
         else: 
          raise TypeError 
        Pc.__init__(self,brand,processor,price) 
        self.battery = battery  
    

    和使用作为

    一样简单
    a = Pc("brand","processor","price") 
    b = Laptop(a, "battery") 
    
  2. 使一个类的方法来处理这种情况

    class Laptop(Pc): 
    
        def __init__(self,brand,processor,price, battery): 
         Pc.__init__(self,brand,processor,price) 
         self.battery = battery 
    
        @classmethod 
        def from_pc_and_battery(cls, pc, battery): 
         return cls(pc.brand, pc.processor, pc.price, battery) 
    

    使用这一块,就是这样

    a = Pc("brand","processor","price") 
    b = Laptop.from_pc_and_battery(a, "battery")