public class Animal{ public Animal(){ System.out.println("I’m an animal."); } } // Bird类是Animal的子类 public class Bird extends Animal{ public Bird(){ System.out.println("I’m a bird."); } public static void main(String[]args){ Bird b = new Bird(); } }
时间: 2023-11-27 17:54:50 浏览: 95
运行这段代码会输出:
```
I’m an animal.
I’m a bird.
```
这是因为 `Bird` 类继承了 `Animal` 类,所以在创建 `Bird` 对象时,会先调用父类的构造方法 `Animal()`,输出 `"I’m an animal."`,然后再调用子类的构造方法 `Bird()`,输出 `"I’m a bird."`。
相关问题
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();
}
}
```
输出结果与原来的代码相同。
public class TestAnimal { static void showPet(Animal a) { a.eat(); } // static void showCat(Homework7.Cat b){ // b.getName(); // b.play(); // } public static void main(String[] args){ Animal[] animal = new Animal[3]; animal[0] = new Bird(); animal[1] = new Fish(); animal[2] = new Cat(); Animal A = animal[0]; Animal B = animal[1]; Animal C = animal[2]; IPet cat = new Cat(); // // String[] dongwu = new String[3]; // dongwu[0] = A.toString(); // dongwu[1] = B.toString(); // dongwu[2] = C.toString(); // for(int i = 0;i<3;i++){ // System.out.println(); // } showPet(A); showPet(B); showPet(C); cat.play(); cat.getName(); } }我要将Bird、Fish、Cat对象对应数组元素,并用循环输出,要怎么改写
您可以将数组元素的类型改为 Animal,并使用 instanceof 运算符判断元素的类型,然后调用对应的方法。以下是修改后的代码:
```
public class TestAnimal {
static void showPet(Animal a) {
a.eat();
}
public static void main(String[] args){
Animal[] animals = new Animal[3];
animals[0] = new Bird();
animals[1] = new Fish();
animals[2] = new Cat();
IPet cat = new Cat();
for (int i = 0; i < animals.length; i++) {
if (animals[i] instanceof Bird) {
System.out.println("This is a bird:");
showPet(animals[i]);
} else if (animals[i] instanceof Fish) {
System.out.println("This is a fish:");
showPet(animals[i]);
} else if (animals[i] instanceof Cat) {
System.out.println("This is a cat:");
showPet(animals[i]);
cat.play();
cat.getName();
}
}
}
}
```
注意,为了调用 Cat 对象的 play() 和 getName() 方法,我们需要将 cat 对象的类型声明为 IPet,而不是 Animal。
阅读全文