当前位置:网站首页>类初始化和实例初始化面试题
类初始化和实例初始化面试题
2022-04-23 02:12:00 【lisin-lee-cooper】
输出程序打印顺序
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.类初始化过程
1一个类要创建实例需要先加载初始化改类,main方法所在的类需要先加载和初始化;
2一个字类需要初始化并先初始化父类;
3一个类初始化就是执行《clinit》()方法:
《clinit》()方法由静态类变量显示赋值代码和静态代码块组成,
《clinit》()方法并向上至下按照顺序执行,
《clinit》()方法只执行一次。
public static void main(String[] args) {
//执行main方法,初始化类,不初始化实例, (4) (1) (9) (6)
}
2.实例初始化过程
执行()方法:
1《init》() 方法可能重载有多个,有几个构造器就有几个《init》() 方法;
2《init》) 方法由非静态实例变量显示赋值代码和非静态代码块,对应构造器组成;
3非静态实例变量显示赋值代码和非静态代码块代码从上到下顺序执行,构造器代码最后执行;
4每次创建实例对象,调用对应的构造器,执行对应的《init》() 方法;
5子类的构造器一定会调用父类的构造器;
public static void main(String[] args) {
Father father = new Son();
father = new Son();
执行main方法,初始化类,初始化实例, (4) (1) (9) (6)(10)(3) (2) (10) (8) (7) (10)(3) (2) (10) (8) (7)
}
3.方法重写
1不可被重写方法:
final
static
private
2对象的多态性:
子类如果重写了父类的方法,通过子类对象调用的一定是子类重写过的代码;
非静态方法默认的调用对象是this;
this对象在构造器或者说()方法中就是正在创建的对象。
版权声明
本文为[lisin-lee-cooper]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47626220/article/details/124354979
边栏推荐
- Summary of I / O knowledge points
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- 002_Redis_String类型常见的操作命令
- How does Axure set the content of the text box to the current date when the page is loaded
- On LAN
- What is a dial-up server and what is its use?
- What categories do you need to know before using proxy IP?
- What business scenarios will the BGP server be used in?
- 一加一为什么等于二
猜你喜欢
013_基于Session实现短信验证码登录流程分析
每日一题(2022-04-21)——山羊拉丁文
Leetcode39 combined sum
【Chrome扩展程序】content_script的跨域问题
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
使用代理IP是需要注意什么?
What is a dial-up server and what is its use?
Nanny level tutorial on building personal home page (II)
They are all intelligent in the whole house. What's the difference between aqara and homekit?
Heap overflow of kernel PWN basic tutorial
随机推荐
How many steps are there from open source enthusiasts to Apache directors?
Flink real-time data warehouse project - Design and implementation of DWS layer
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
[NK] Niuke monthly race 48 D
Shardingsphere sub database and sub table
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
013_ Analysis of SMS verification code login process based on session
R language advanced | generalized vector and attribute analysis
【Chrome扩展程序】content_script的跨域问题
不断下沉的咖啡业,是虚假的繁荣还是破局的前夜?
我国科学家揭示突破水稻产量瓶颈新机制
Find the largest number of two-dimensional arrays
使用代理IP是需要注意什么?
What are the common proxy IP problems?
Consider defining a bean of type 'com netflix. discovery. AbstractDiscoveryClientOptionalArgs‘
PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]
JSP page nesting
C standard library - < time h>
Wechat public platform test number application, authorized login function and single sign on using hbuilder X and wechat developer tools
PTA: 点赞狂魔