package com.yingcheng1101.object_oriented.newobject;
//用于给Strudent,Person对象成员属性显示初始化时显示执行了动作的消息类
class InformationDisplay {
public InformationDisplay(String str) {// 对象初始化时打印指定的消息
System.out.println(str);
}
}
public class Student extends Person {
private InformationDisplay behavior = new InformationDisplay("behavior的显示初始化");
{// 构造代码块
behavior = new InformationDisplay("Student构造代码块对behavior的操作");
}
Student() {// 构造函数
super();// super()语句的执行说明Student类构造函数的执行
// 在super()从Person类的构造函数返回来之后,立马就完成了behavior的显示初始化话动作
// 之后就是Student构造代码块的执行
behavior = new InformationDisplay("Student构造函数语句的开始执行");// 这时才开始构造函数里面语句的执行
System.out.println("Student Constructor over");
}
public static void main(String[] args) {
new Student();
}
}
class Person {
private InformationDisplay action = new InformationDisplay("action的显示初始化");
{// 构造代码块
action = new InformationDisplay("Person构造代码块对action的操作");
}
// Student对象通过super()访问父类的构造函数
Person() {// 构造函数
// 在super()从Object类的构造函数返回来之后,立马就完成了action的显示初始化话动作
// 之后就是Person构造代码块的执行
action = new InformationDisplay("Person构造函数语句的开始执行");// 这时才开始构造函数里面语句的执行
System.out.println("Person Constructor over");
}
}
// action的显示初始化
// Person构造代码块对action的操作
// Person构造函数语句的开始执行
// Person Constructor over
// behavior的显示初始化
// Student构造代码块对behavior的操作
// Student构造函数语句的开始执行
// Student Constructor over
new关键字创建的对象的实例化过程
最新推荐文章于 2024-01-18 09:24:06 发布