thinking in java test5.8练习(16)(17)(18)

练习(16):创建一个String对象数据,并为每一个元素都复制一个String。用for循环来打印该数组。

public static void main(String[] args){
        String[] str = new String[5];
        str[0] = "杭州";
        str[1] = "举办";
        str[2] = "G20";
        str[3] = "峰会";
        str[4] = "!!!";
        for (String s :
                str) {
            System.out.print(s);
        }
    }

练习(17):创建一个类,它有一个接受一个String参数的构造器,在构造阶段,打印该参数。创建一个该类的对象引用数组,但是不实际去创建对象赋值给该数组。当运行程序时,请注意来自对该构造器的调用中的初始化消息是否打印了出来。
这个看完题目就知道不会打印,对象都没有创建肯定不会调用构造函数。

public class test5_8_17 {
    public static void main(String[] args){
        test17[] test17s = new test17[5];
    }
}
class test17{
    public test17(String str){
        System.out.println(str);
    }
}

练习(18):通过创建对象赋值给引用数组,从而完成前一个练习。

public class test5_8_17 {
    public static void main(String[] args){
        test17[] test17s = new test17[5];
        test17s[0] = new test17("0");
        test17s[1] = new test17("1");
        test17s[2] = new test17("2");
        test17s[3] = new test17("3");
        test17s[4] = new test17("4");
    }
}
class test17{
    public test17(String str){
        System.out.println(str);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值