您的位置:首页 > 编程语言 > Java开发

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

2016-09-04 10:34 381 查看
练习(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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java