【问题标题】:How is polymophism working in Python if parent constructor is not invoked (unlike Java)?如果不调用父构造函数(与 Java 不同),多态性在 Python 中如何工作?
【发布时间】:2020-07-24 20:00:41
【问题描述】:

因此,在 Java 中调用父类构造函数,但在 Python 中不调用。如果这意味着没有创建父对象,那么如何在 Python 中成功调用 def function - 这里发生了什么?

Python 代码

class Parent:
    def __new__(self):
        print(f"I am the real parent constructor Hahahah {self}")
        return object.__new__(self)

    def __init__(self):
        print(f"I am the constructor of parent {self}")

    def function(self):
        print(f"I am parent's member function and my self value is {self}")

    def over(self):
        print(f"I am parent's O-function and my self value is {self}")

class Child(Parent):
    def __new__(self):
        print(f"I am the real chid constructor Hahahah {self}")
        return object.__new__(self)

    def __init__(self):
        print(f"I am the initialize of child {self}")

    def over(self):
        print(f"I am the child's member O-function and my self value is {self}")

ch = Child()
ch.over()
ch.function()

上述 Python 代码的输出。 注意:I am the real parent constructor Hahahah 没有打印出来。

I am the real chid constructor Hahahah <class '__main__.Child'>
I am the initialize of child <__main__.Child object at 0x7f4bb5d997b8>
I am the child's member O-function and my self value is <__main__.Child object at 0x7f4bb5d997b8>
I am parent's member function and my self value is <__main__.Child object at 0x7f4bb5d997b8>

类似的 Java 代码

public class main {

    public static void main(String[] args) {
        Child ch = new Child();
        ch.over();
        ch.function();
    }
}

class Parent {
    Parent () {
        System.out.println("In the parent class constructor | " + this);
    }

    public void function () {
        System.out.println("In the member function of parent | " + this);
    }

    public void over () {
        System.out.println("In the member O-function of parent | " + this);
    }
}

class Child extends Parent {
    Child () {
        System.out.println("I the child class constructor | " + this);
    }

    public void over () {
        System.out.println("In the member O-function of chlid | " + this);
    }
}

上述 Java 代码的输出

In the parent class constructor | code.Child@2a139a55
I the child class constructor | code.Child@2a139a55
In the member O-function of chlid | code.Child@2a139a55
In the member function of parent | code.Child@2a139a55

【问题讨论】:

    标签: java python inheritance polymorphism


    【解决方案1】:

    Python 和 Java 是不同的。

    在 Java 中,扩展类必须始终调用父构造函数。为了方便起见,如果父构造函数没有参数,它将首先自动调用。现在,如果您要向 Java Parent 构造函数添加一个参数,如下所示:

    Parent (int i) {
        System.out.println("In the parent class constructor | " + this);
    }
    

    你会发现如下编译错误:

    There is no default constructor available in 'org.example.Parent'
    

    这是有道理的,当构造 Child Java 不知道将什么作为值 i 传递时。所以我们必须手动调用Parent构造函数:

    Child () {
        super(1);
        System.out.println("I the child class constructor | " + this);
    }
    

    Python 不那么严格。始终调用父构造函数仍然是一种好习惯,但 Python 不会要求您这样做。这是因为 Python 不是 type safe

    现在让我们看看如果忘记调用父构造函数会发生什么:

    class Parent:
        def __init__(self):
            self.i = 1
            print(f"I am the constructor of parent {self}")
    
        def printi(self):
            print(self.i)
    
    class Child(Parent):
    
        def __init__(self):
            pass
    
    ch = Child()
    ch.printi()
    

    会发生致命错误:

    Traceback (most recent call last):
      File "test.py", line 15, in <module>
        ch.printi()
      File "test.py", line 7, in printi
        print(self.i)
    AttributeError: 'Child' object has no attribute 'i'
    

    要修复此代码,您可以将以下行添加到子 init 函数:

    Parent.__init__(self)
    

    正如您所注意到的,Parent 类仍然被创建(因为您可以调用 function 方法。这又是因为 Python 不那么严格。In python an object can be created without calling the constructor.

    【讨论】:

      猜你喜欢
      • 2016-12-09
      • 2018-02-28
      • 2021-06-18
      • 2017-11-29
      • 2018-03-29
      • 2021-05-08
      • 1970-01-01
      • 2011-09-30
      相关资源
      最近更新 更多