shape
当多个类之间有继承关系时,创建子类对象会导致父类初始化块的执行。
package 类与继承;
public class Test {
public static void main(String[] args) {
Shape shape = new Circle();//初始化,调用
System.out.println(shape.name);
shape.printType();//构造函数
shape.printName();
}
}
class Shape {
public String name = "shape";//定义name的数据类型,并赋值
public Shape(){
//constructor 属性返回对创建此对象的数组函数的引用
System.out.println("shape constructor");
}
public void printType() {
System.out.println("this is shape");
}
public static void printName() {
System.out.println("shape");
}
}
//继承父类Shape
class Circle extends Shape {
public String name = "circle";
public Circle() {
System.out.println("circle constructor");
}
public void printType() {
System.out.println("this is shape");//输出角
}
public static void printName() {
System.out.println("circle");
}
}