java中构造方法,方法体加入System.out.println()会如何?

如果在构造方法体中写一个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"
    }
}

结果为: 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值