


When creating a derived class object, what is the execution order of constructors?
Jul 08, 2020 pm 03:55 PMWhen creating a derived class object, the execution order of the constructor is: base class constructor, derived class object member constructor, and the constructor of the derived class itself. When an object is created, the code loading order is: static code, non-static code, and constructor methods.
When creating a derived class object, the execution order of the constructor is: base class constructor, derived class object member constructor, and the constructor of the derived class itself.
(Recommended tutorial: java introductory program)
Detailed introduction:
The loading order of the code when the object is created is: static code --> Non-static code --> Constructor.
If the parent class is inherited, the loading order is: the static code of the parent class --> the static code of the subclass --> the non-static code within the parent class --> the parent class's Constructor --> Non-static code of the subclass --> Constructor of the subclass.
Among them, static code includes (static methods, static variables, static code blocks, etc.), non-static code is (member methods, member variables, member code blocks, etc.). The same type of code, written above load.
(Video tutorial recommendation: java video tutorial)
Example:
public class ExtendsTest { public static void main(String[] args) { // TODO Auto-generated method stub C c = new D(); } } class C { static { System.out.println("C 基類靜態(tài)域 "); } { System.out.println("C 基類對(duì)象成員構(gòu)造函數(shù)"); } public C() { System.out.println("C 基類本身的構(gòu)造函數(shù)"); } } class D extends C { static { System.out.println("D 派生類靜態(tài)域"); } { System.out.println("D 派生類對(duì)象成員構(gòu)造函數(shù)"); } public D() { System.out.println("D 派生類本身的構(gòu)造函數(shù)"); } }
Output result:
The above is the detailed content of When creating a derived class object, what is the execution order of constructors?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

JSON (JavaScriptObjectNotation) is a lightweight data exchange format that has become a common format for data exchange between web applications. PHP's json_encode() function can convert an array or object into a JSON string. This article will introduce how to use PHP's json_encode() function, including syntax, parameters, return values, and specific examples. Syntax The syntax of the json_encode() function is as follows: st

Use Python's __contains__() function to define the containment operation of an object. Python is a concise and powerful programming language that provides many powerful features to handle various types of data. One of them is to implement the containment operation of objects by defining the __contains__() function. This article will introduce how to use the __contains__() function to define the containment operation of an object, and give some sample code. The __contains__() function is Pytho

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

Title: Using Python's __le__() function to define a less than or equal comparison of two objects In Python, we can define comparison operations between objects by using special methods. One of them is the __le__() function, which is used to define less than or equal comparisons. The __le__() function is a magic method in Python and is a special function used to implement the "less than or equal" operation. When we compare two objects using the less than or equal operator (<=), Python

The difference between Java heap and stack and application scenario analysis require specific code examples. In Java programs, heap and stack are two commonly used data structures, and they assume different roles and functions in memory. Understanding the difference between heap and stack is crucial to writing efficient Java programs. First, let's take a look at the Java heap. The heap is an area used to store objects. All objects created in the program are stored in the heap. The heap is where memory is dynamically allocated and released while the program is running. It is not subject to any restrictions and can be automatically allocated and released as needed.

How to use the getattr() function in Python to obtain the attribute value of an object. In Python programming, we often encounter situations where we need to obtain the attribute value of an object. Python provides a built-in function getattr() to help us achieve this goal. The getattr() function allows us to get the attribute value of an object by passing the object and attribute name as parameters. This article will introduce the usage of getattr() function in detail and provide actual code examples for better understanding. g
