如果在构造方法体中写一个System.out.println(" ***");
那在创建对象时调用构造方法,会发生什么呢?
public class textCar {
int x;
public textCar(){
System.out.println("听心湖水怪");
}
}
class a{
public static void main(String[] args){
text t= new textCar();
}
}
输出结果:
再进一步,如果是有个子类参与进来。父类引用指向创建的子类对象呢?结果会怎么样?
public class textCar {
int x;
public textCar(){
System.out.println("听心湖水怪");
}
}
class Bike extends textCar{
int a;
public Bike(){
System.out.println("bike");
}
}
class a{
public static void main(String[] args){
textCar t= new textCar();//这里输出一个
textCar x=new Bike();//这里输出俩 :"听心湖水怪"、"bike"
}
}
结果为: