关于下面代码,说法正确的是: class Cat { Cat (int c) { System.out.print ("cat"+c+" ");//1 } } class SubCat extends Cat { SubCat (int c){ super (5); //2 System.out.print ("cable");//3 } SubCat() { this(4);//4 } public static void main (String [] args) { Cat s= new SubCat(); } } A. 类Cat有一个默认的空的无参构造方法。 B. 标注为//2和//3的语句可以调换顺序。 C. 标注为//1,//2,//3,//4的语句先后执行顺序为4, 2, 1, 3 D. 标注为//1,//2,//3,//4的语句的先后执行顺序为4, 2, 3, 1
时间: 2024-01-14 14:04:53 浏览: 13
答案是 A。因为类Cat没有声明任何构造方法,所以编译器会默认生成一个空的无参构造方法。同时,标注为//1,//2,//3,//4的语句的先后执行顺序为4, 2, 1, 3。在创建SubCat对象时,会调用SubCat的构造方法,由于没有显式调用父类构造方法,所以会默认调用父类的无参构造方法,输出"cat5"。然后执行标注为//3的语句,输出"cable"。因此正确答案是A选项。
相关问题
public class Animal { String[] animals = {"dog", "cat", "bird", "fish"}; public void printAnimals() { for (String animal : animals) { System.out.println(animal); } }}public class Dog extends Animal { int[] nums = {1, 2, 3, 4}; public void printNums() { for (int num : nums) { System.out.println(num); } }}public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.printAnimals(); // 输出:dog cat bird fish dog.printNums(); // 输出:1 2 3 4 }}如果这个代码中的数组是在Main中建立该如何解决
可以将数组作为参数传递给Animal和Dog类的构造函数,并在构造函数中初始化数组。代码示例:
```
public class Animal {
String[] animals;
public Animal(String[] animals) {
this.animals = animals;
}
public void printAnimals() {
for (String animal : animals) {
System.out.println(animal);
}
}
}
public class Dog extends Animal {
int[] nums;
public Dog(String[] animals, int[] nums) {
super(animals);
this.nums = nums;
}
public void printNums() {
for (int num : nums) {
System.out.println(num);
}
}
}
public class Main {
public static void main(String[] args) {
String[] animals = {"dog", "cat", "bird", "fish"};
int[] nums = {1, 2, 3, 4};
Dog dog = new Dog(animals, nums);
dog.printAnimals();
dog.printNums();
}
}
```
输出结果与原来的代码相同。
已知有如下Animal抽象类,请编写其子类Dog类与Cat类,另外再编写一个生产动物的Factory工厂类,具体要求如下。 已有的Animal抽象类定义: abstract class Animal{ private String name; //名字 private int age; //年龄 public abstract void info(); //返回动物信息 public abstract void speak(); //动物叫 public Animal(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 需要你编写的Dog子类: 增加violence(凶猛程度)属性(int型),重写info和speak方法 info方法输出Dog的name、age和violence属性,输出格式样例为:该狗的名字是Mike,年龄是2岁,凶猛程度是78度 (注意:输出结果中没有空格,逗号为英文标点符号) speak方法输出Dog 的叫声,输出格式样例为:旺旺 需要你编写的Cat子类: 增加mousingAbility(捕鼠能力)属性(int型),重写info和speak方法 info方法输出Cat的name、age和mousingAbility属性,输出格式样例为:该猫的名字是Kitty,年龄是4岁,捕鼠能力是88分 (注意:输出结果中没有空格,逗号为英文标点符号) speak方法输出Cat 的叫声,输出格式样例为:喵喵 需要你编写的Factory工厂类: Animal getAnimalByType(int type,String name, int age, int ownAttribute):根据传入的子类类型和属性值,返回一个Animal类型的上转型对象,type为1代表狗类,type为2代表猫类
。如果传入的type值不为1或2,则返回null。
Factory类的代码如下:
public class Factory {
public Animal getAnimalByType(int type, String name, int age, int ownAttribute) {
if (type == 1) {
return new Dog(name, age, ownAttribute);
} else if (type == 2) {
return new Cat(name, age, ownAttribute);
} else {
return null;
}
}
}
Dog子类的代码如下:
public class Dog extends Animal {
private int violence;
public Dog(String name, int age, int violence) {
super(name, age);
this.violence = violence;
}
public void info() {
System.out.print("该狗的名字是" + getName() + ",年龄是" + getAge() + ",凶猛程度是" + violence + "度");
}
public void speak() {
System.out.print("旺旺");
}
}
Cat子类的代码如下:
public class Cat extends Animal {
private int mousingAbility;
public Cat(String name, int age, int mousingAbility) {
super(name, age);
this.mousingAbility = mousingAbility;
}
public void info() {
System.out.print("该猫的名字是" + getName() + ",年龄是" + getAge() + ",捕鼠能力是" + mousingAbility + "分");
}
public void speak() {
System.out.print("喵喵");
}
}
以上就是根据题目要求写出的Animal抽象类、Dog子类、Cat子类以及Factory工厂类的代码。通过Factory类获取Animal对象时,传入的type参数可以用一个变量来接收用户的输入,然后根据实际情况调用Factory类的getAnimalByType方法来获取相应的Animal对象。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)