使用较少的按键进行子类化

2024-06-08 23:29:16 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用超类的实例定义子类而不需要大量输入?请参见:

class A():
    def __init__(self,x,y):
        self.x=x
        self.y=y

class B(A):
    def __init__(self,x,y,z):
        A.__init__(A,x,y)
        self.z=z

class1=A(2,3)
class2=B(class1.x,class1.y,5)   # Is there something easier than this, where I don't have to type class1.x, class1.y and
                                # I can just pass all the data members of class1 at once?

Tags: 实例self定义initisdefthis子类
1条回答
网友
1楼 · 发布于 2024-06-08 23:29:16
class A(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y

class B(A):
    def __init__(self,a,z):
        self.a=a
        self.z=z

class1=A(2,3)
class2=B(class1,5)

相关问题 更多 >