编程要求
仔细阅读右侧编辑区内给出的代码框架及注释,按照提示编写程序代码。 测试说明
平台将使用测试集运行你编写的程序代码,若全部的运行结果正确,则通关。 可在右侧“测试结果”区查看具体的测试集详情。
答案
/**
* 任务:使用对象数组的方式创建 3 个 Dog 对象
* 类名为:Dog
* 该类为 Dog 的基本属性
*/
/**
* 任务:使用对象数组的方式创建 3 个 Dog 对象
* 类名为:Dog
*/
public class Dog {
private String name; // 小狗名称
private String type; // 小狗品种
private int age; // 小狗年龄
private String hobby; //小狗爱好
public Dog(){
}
public Dog(String name, String type, int age, String hobby) {
this.name = name;
this.type = type;
this.age = age;
this.hobby = hobby;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public void info(){
System.out.printf("小狗名称:%s\n品种:%s\n小狗年龄:%d\n小狗爱好:%s\n",name,type,age,hobby);
}
public static void main(String[] args) {
Dog d1 = new Dog("Tom", "哈士奇",2,"拆家");
Dog d2 = new Dog("jerry", "中华田园犬",3,"护家");
Dog d3 = new Dog("旺财","柯基",2,"吃喝玩");
// 请在下面的Begin-End之间按照注释中给出的提示编写正确的代码
/********** Begin **********/
// 将三个狗的对象放进对象数组中,并依次打印该对象的info方法
Dog dogs[];
dogs = new Dog[3];
dogs[0] = d1;
dogs[1] = d2;
dogs[2] = d3;
for (int i = 0; i < dogs.length; i++) {
dogs[i].info();
}
/********** End **********/
}
}