class Animal { public void sound() { System.out.println("动物发出声音"); } } class Dog extends Animal { public void sound() { System.out.println("狗发出吠声"); } } class Cat extends Animal { } public class Test { public static void main(String args[]) { Animal animal = new Animal(); // Animal 对象 Animal dog = new Dog(); // Dog 对象 Animal cat = new Cat(); // Cat 对象 animal.sound(); dog.sound(); cat.sound(); } }的输出是什么
时间: 2024-01-06 15:04:01 浏览: 30
输出结果为:
```
动物发出声音
狗发出吠声
动物发出声音
```
解析如下:
- `animal.sound()` 调用的是 `Animal` 类中的 `sound()` 方法,输出“动物发出声音”。
- `dog.sound()` 调用的是 `Dog` 类中重写的 `sound()` 方法,输出“狗发出吠声”。
- `cat.sound()` 调用的是 `Cat` 类继承自 `Animal` 类的 `sound()` 方法,因为 `Cat` 类没有重写此方法,所以输出“动物发出声音”。
相关问题
class Animal { public void sound() { System.out.println("动物发出声音"); } } class Dog extends Animal { public void sound() { System.out.println("狗发出吠声"); } } class Cat extends Animal { public void sound() { } public class Test { public static void main(String args[]) { Animal animal = new Animal(); // Animal 对象 Animal dog = new Dog(); // Dog 对象 Animal cat = new Cat(); // Cat 对象 animal.sound(); dog.sound(); cat.sound(); } }的输出是什么
输出结果是:
```
动物发出声音
狗发出吠声
动物发出声音
```
在这个程序中,Animal 是一个父类,而 Dog 和 Cat 是 Animal 的子类。子类可以继承父类的属性和方法,并且可以重写父类的方法。在这个程序中,Animal 类中有一个 sound() 方法,而 Dog 和 Cat 类都继承了这个方法并对其进行了重写。在 main 方法中,分别创建了 Animal、Dog 和 Cat 类的对象,并调用了它们的 sound() 方法。animal 对象调用的是 Animal 类的 sound() 方法,而 dog 和 cat 对象调用的是它们各自的 sound() 方法。因此,输出结果为动物发出声音、狗发出吠声、动物发出声音。
class Animal { } class Dog extends Animal { public void sound() { System.out.println("狗发出吠声"); } } class Cat extends Animal { public void sound() { System.out.println("猫发出喵声"); } } public class Test { public static void main(String args[]) { Animal animal = new Animal(); // Animal 对象 Animal dog = new Dog(); // Dog 对象 Animal cat = new Cat(); // Cat 对象 animal.sound(); dog.sound(); cat.sound(); } }的输出是什么
输出结果为:
```
(什么也不会输出,因为 Animal 类中没有 sound() 方法)
狗发出吠声
猫发出喵声
```
在这个程序中,Animal 是一个父类,而 Dog 和 Cat 是 Animal 类的子类。在 main 方法中,我们创建了一个 Animal 对象和两个 Animal 的子类对象(Dog 和 Cat)。当我们调用 sound() 方法时,会根据对象的实际类型来执行相应的方法。因此,对于 Animal 对象,sound() 方法没有被重写,因此什么也不会输出;对于 Dog 和 Cat 对象,sound() 方法被重写了,分别输出狗发出吠声和猫发出喵声。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)