当前位置:网站首页>Class initialization and instance initialization interview questions
Class initialization and instance initialization interview questions
2022-04-23 02:14:00 【lisin-lee-cooper】
Output program print order
public class Father {
private int a = testA();
private static int b = testB();
static {
System.out.println("(1)");
}
Father() {
System.out.println("(2)");
}
{
System.out.println("(3)");
}
private static int testB() {
System.out.println("(4)");
return 0;
}
public int testA() {
System.out.println("(5)");
return 0;
}
}
public class Son extends Father {
private int a = testA();
private static int b = testB();
static {
System.out.println("(6)");
}
Son() {
System.out.println("(7)");
}
{
System.out.println("(8)");
}
private static int testB() {
System.out.println("(9)");
return 0;
}
public int testA() {
System.out.println("(10)");
return 0;
}
}
1. Class initialization process
1 To create an instance of a class, you need to load the initialization class first ,main The class of the method needs to be loaded and initialized first ;
2 A word class needs to be initialized and the parent class needs to be initialized first ;
3 A class initialization is execution 《clinit》() Method :
《clinit》() Method consists of static class variable display assignment code and static code block ,
《clinit》() Methods and execute them in order from top to bottom ,
《clinit》() Method is only executed once .
public static void main(String[] args) {
// perform main Method , Initialization class , Do not initialize instance , (4) (1) (9) (6)
}
2. Instance initialization process
perform () Method :
1《init》() Method may overload more than one , There are several constructors, there are several 《init》() Method ;
2《init》) Method displays the assignment code and the non static code block by the non static instance variable , Composition of corresponding constructor ;
3 The non static instance variable shows that the assignment code and the non static code block code are executed from top to bottom , The constructor code finally executes ;
4 Every time you create an instance object , Call the corresponding constructor , Execute corresponding 《init》() Method ;
5 The constructor of the subclass must call the constructor of the parent class ;
public static void main(String[] args) {
Father father = new Son();
father = new Son();
perform main Method , Initialization class , Initialize instance , (4) (1) (9) (6)(10)(3) (2) (10) (8) (7) (10)(3) (2) (10) (8) (7)
}
3. Method rewriting
1 Methods that cannot be overridden :
final
static
private
2 Polymorphism of objects :
Subclass if the method of the parent class is overridden , Through the subclass object call must be subclass rewritten code ;
The default call object for non static methods is this;
this The object is in the constructor or () Method is the object being created .
版权声明
本文为[lisin-lee-cooper]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230212276775.html
边栏推荐
- 【Chrome扩展程序】content_script的跨域问题
- C语言中*与&的用法与区别 以及关键字static和volatile 的含义
- Usage of vector common interface
- C # import details
- [assembly language] understand "stack" from the lowest point of view
- Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
- The importance of ERP integration to the improvement of the company's system
- Applet reads files
- Dynamic batch processing and static batch processing of unity
- 89 logistic regression user portrait user response prediction
猜你喜欢

Use Xdebug breakpoint debugging in postman

Micro build low code zero foundation introductory course

013_ Analysis of SMS verification code login process based on session

What is BGP server and what are its advantages?

006_redis_SortedSet类型

Shardingsphere broadcast table and binding table

PTA: 点赞狂魔

Usage of vector common interface

【Chrome扩展程序】content_script的跨域问题

World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
随机推荐
Explain JS prototype and prototype chain in detail
[leetcode daily question] 396 Rotation function
Analyze the three functions of static proxy IP.
What are the test steps of dynamic proxy IP?
How to call out services in idea and display the startup class in services
tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
Use of push() and pop()
Shardingsphere broadcast table and binding table
006_ redis_ Jedis quick start
tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
day18--栈队列
go语言打怪通关之 ⌈互斥锁和状态协程⌋
Latin goat (20204-2022) - daily question 1
013_基于Session实现短信验证码登录流程分析
What is a dial-up server and what is its use?
LeetCode 349. Intersection of two arrays (simple, array) Day12
001_ Redis set survival time
每日一题(2022-04-21)——山羊拉丁文
[nk]牛客月赛48 D
都是做全屋智能的,Aqara和HomeKit到底有什么不同?