Calling member functions from a constructor

后端 未结 5 899
我在风中等你
我在风中等你 2020-12-29 08:17

I know this question has a similar title to this: C++: calling member functions within constructor? but I am asking a more general question.

Is it good practice to c

5条回答
  •  青春惊慌失措
    2020-12-29 08:41

    I don't think there is anything inherently wrong in calling member functions from a constructor provided that they are not virtual functions.

    The problem with calling virtual member functions from a constructor is that a subclass can override the function. This will cause the constructor to call the overridden implementation in the subclass, before the constructor for the subclass part of the object has been called.

    In Java, any one of the private, static or final access modifiers will make the method safe to call from a constructor by preventing a virtual call to the superclass method. I don't think these techniques are available in Python.

提交回复
热议问题