Javajava inheriting basic styles
Class Demo extends object{ demo (int a) {this (); } Demo () { super (); }}
Java inherits the object class by default and is bound to invoke the super () method in the constructor to instantiate the parent class. Note: this () and super () cannot appear in the same constructor at the same time, but super () must appear in one of the constructors!
Java Instantiation Process
- Run the static code portion of the parent class
- Rerun the static code portion of the subclass
- Executing the parent class member code
- Executes the parent class constructor--The parent class completes the instantiation
- To execute a member code block of a subclass
- Constructors for executing subclasses--instantiation of subclasses complete
Some details of the Java instantiation process
Pythonpython inheriting basic styles
Class Demo (object): def __init__ (self,a): super (). __init__ () SELF.A = a
The Python instantiation process is the same as Java, which inherits object by default and requires that the parent class be instantiated.
Python instantiation Process
- Executing static code for the parent class
- Executing a subclass of static code
- Execute Subclass __new__ () method
- Calling the parent class __new__ () method in the subclass __new__ () method
- Call the parent class __init__ () method (parent instantiation complete)
- Call Subclass __init__ () method (subclass instantiation complete)
python instantiation Process
Contrast inheritance
When a Java object calls a method in the parent class, the method called by the method takes precedence over the method of the subclass, and the member variable that is called takes precedence over the variable of the parent class.
When a Python object calls a method in the parent class, the methods and variables that are called take precedence over the methods and variables of the child class.
Polymorphic
Python Natural polymorphism
Java: Because of the problem of variable properties, you must demote and convert the objects to use the methods and properties of the child classes.
Animal a = new Cat ();//a does not use the Cat class function, all properties and methods are Animal the parent class of cat C = (cat) A; To use
In the example above, when the variable type is the parent class or interface of the instance type, there are differences in compile and run:
- Member variables: Both compile and run refer to animal = left!
- Method: Compile Reference animal = left, parent class has only compile pass, run priority reference Cat is reference = right. (The method of a subclass is still used in the case of duplicate names, but if you call a method that the parent class does not have, it cannot be compiled.) )
- Static part: Because there is no need for the object, there is no this, are reference animal = left!
Java, Python object-oriented inheritance and its differences