当前位置:网站首页>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
边栏推荐
- Applet reads files
- Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
- How does Axure set the content of the text box to the current date when the page is loaded
- Analyze the three functions of static proxy IP.
- They are all intelligent in the whole house. What's the difference between aqara and homekit?
- Lane cross domain problem
- Dynamic batch processing and static batch processing of unity
- Flink real-time data warehouse project - Design and implementation of DWS layer
- LeetCode 283. Move zero (simple, array) Day12
- hyperscan --- 1
猜你喜欢
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
005_ redis_ Set set
006_redis_SortedSet类型
Easyswool environment configuration
011_ Redistemplate operation hash
001_redis设置存活时间
arduino esp8266 网络升级 OTA
VMware virtual machine installation openwrt as side route single arm route img image to vmdk
How to choose a good dial-up server?
随机推荐
wordpress 调用指定页面内容详解2 get_children()
How to choose a good dial-up server?
leetcode:27. 移除元素【count remove小操作】
Redis memory recycling strategy
How to configure iptables to realize local port forwarding
013_基于Session实现短信验证码登录流程分析
011_ Redistemplate operation hash
[nk]牛客月赛48 D
Develop a chrome plug-in from 0 (2)
What businesses use physical servers?
SO库依赖问题
How does Axure set the content of the text box to the current date when the page is loaded
Applet canvas canvas half ring
C语言中*与&的用法与区别 以及关键字static和volatile 的含义
007_Redis_Jedis连接池
Latin goat (20204-2022) - daily question 1
A simple and open source navigation website source code
How many steps are there from open source enthusiasts to Apache directors?
PTA: 点赞狂魔
89 logistic regression user portrait user response prediction