public class WrapperClass {
private static class A {
static {
System.out.println("类A初始化开始...");
}
//父类包含子类的static引用
private static B b = new B();
protected static int aInt = 9;
static {
System.out.println("类A初始化结束...");
}
public static void main(String[] args) {
}
}
private static class B extends A {
static {
System.out.println("类B初始化开始...");
}
//子类的域依赖于父类的域
private static int bInt = 9 + A.aInt;
public B() {
//构造器依赖类的static域
System.out.println("类B的构造器调用 " + "bInt的值" + bInt);
}
static {
System.out.println("类B初始化结束... " + "aInt的值:" + bInt);
}
public static void main(String[] args) {
}
}
}
/** * 类A初始化开始... * 类B的构造器调用 bInt的值0 * 类A初始化结束... * 类B初始化开始... * 类B初始化结束... aInt的值:18 */
/** * 类A初始化开始... * 类B初始化开始... * 类B初始化结束... aInt的值:9 * 类B的构造器调用 bInt的值9 * 类A初始化结束... */
public class WrongInstantiation {
private static class A {
public A() {
doSomething();
}
protected void doSomething() {
System.out.println("A's doSomething");
}
}
private static class B extends A {
private int bInt = 9;
@Override
protected void doSomething() {
System.out.println("B's doSomething, bInt: " + bInt);
}
}
public static void main(String[] args) {
B b = new B();
}
}
/** * B's doSomething, bInt: 0 */
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有