当前位置:网站首页>类初始化和实例初始化面试题
类初始化和实例初始化面试题
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
边栏推荐
猜你喜欢

都是做全屋智能的,Aqara和HomeKit到底有什么不同?

What are the common proxy IP problems?

Esp32 message queue using FreeRTOS

Is it better to use a physical machine or a virtual machine to build a website?

007_ Redis_ Jedis connection pool

每日一题(2022-04-22)——旋转函数

Heap overflow of kernel PWN basic tutorial

006_redis_jedis快速入门

Latin goat (20204-2022) - daily question 1

Is the availability of proxy IP equal to the efficiency of proxy IP?
随机推荐
Numerical remapping method (remap)
一加一为什么等于二
Startup of openstack service
What are the common proxy IP problems?
PHP & laravel & master several ways of generating token by API and some precautions (PIT)
The importance of ERP integration to the improvement of the company's system
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
Common formatting problems after word writing
世界读书日 | 技术人不要错过的好书(IT前沿技术)
They are all intelligent in the whole house. What's the difference between aqara and homekit?
Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
Consider defining a bean of type 'com netflix. discovery. AbstractDiscoveryClientOptionalArgs‘
从0开始开发一个chrome插件(2)
代理IP可用率是不是等同于代理IP的效率?
013_基于Session实现短信验证码登录流程分析
Shardingsphere introduction and sub table usage
How to choose a good dial-up server?
校园转转二手市场源码
Dynamic batch processing and static batch processing of unity
009_Redis_RedisTemplate入门