public interface Animal{ void voice(); } class Dog implements Animal{ public void voice(){System.out.println(“WWW!”);} } class Cat implements Animal{ public void voice(){ System.out.println(“MMM!”);} } class Store{ public static Animal get(String choice){ if(choice.equals (“dog”)) return new Dog(); else return new Cat(); } } public class AnimalTest{ public static void main(String[] args){ Animal an=Store.get(“dog”); an.voice(); } }运行结果
时间: 2024-03-25 17:37:51 浏览: 19
运行结果应该是:
```
WWW!
```
因为在 `AnimalTest` 类中调用了 `Store.get("dog")` 方法,该方法会返回一个 `Dog` 对象,然后将其赋值给 `Animal` 类型的变量 `an`。由于 `Dog` 类实现了 `Animal` 接口并重写了 `voice()` 方法,因此在调用 `an.voice()` 方法时会执行 `Dog` 类中的 `voice()` 方法,输出 `WWW!`。
相关问题
以下程序是否能够运行? 如能,按顺序写出输出结果?如不能,错在哪,如何改。interface Animal { void breathe (); void run (); void eat (); class Dog implements Animal { public void breathe (){ System . out . println (" I ' m breathing "); void eat (){ System . out . println (" I ' m eating "); class Test { public static void main ( String [] args ){ Dog dog = new Dog (); dog . breathe (); dog . eat (); ~ m
这段程序无法运行,因为接口 `Animal` 中的方法没有被正确地实现和声明。以下是修改后的程序:
```
interface Animal {
void breathe();
void run();
void eat();
}
class Dog implements Animal {
public void breathe() {
System.out.println("I'm breathing");
}
public void run() {
System.out.println("I'm running");
}
public void eat() {
System.out.println("I'm eating");
}
}
class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.breathe();
dog.run();
dog.eat();
}
}
```
输出结果为:
```
I'm breathing
I'm running
I'm eating
```
以下程序是否能够运行? 如能,按顺序写出输出结果?如不能,错在哪,如何改。 interface Animal { void breathe (); void run (); void eat (); } class Dog implements Animal { public void breathe (){ System . out . println (" I ' m breathing "); } void eat (){ System . out . println (" I ' m eating "); } } class Test { public static void main ( String [] args ){ Dog dog = new Dog (); dog . breathe (); dog . eat (); } }
这段程序无法运行,因为 `Dog` 类中的 `eat()` 方法没有被正确地实现和声明。以下是修改后的程序:
```
interface Animal {
void breathe();
void run();
void eat();
}
class Dog implements Animal {
public void breathe() {
System.out.println("I'm breathing");
}
public void run() {
System.out.println("I'm running");
}
public void eat() {
System.out.println("I'm eating");
}
}
class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.breathe();
dog.eat();
}
}
```
输出结果为:
```
I'm breathing
I'm eating
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)