public class Animal { public void eat() { System.out.println("Animal is eating"); } } public class Dog extends Animal { public void bark() { System.out.println("Dog is barking"); } } // 在另一个文件中 public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.eat(); // 输出 "Animal is eating" dog.bark(); // 输出 "Dog is barking" } }。解释一下这段代码
时间: 2023-04-08 20:01:52 浏览: 167
这段代码是一个 Java 程序,定义了一个 Animal 类和一个 Dog 类,Dog 类继承了 Animal 类。Animal 类中有一个 eat() 方法,Dog 类中有一个 bark() 方法。在 Main 类中创建了一个 Dog 对象,调用了它的 eat() 和 bark() 方法,分别输出 "Animal is eating" 和 "Dog is barking"。
相关问题
public class Animal { protected final String name; public Animal(String name) { this.name = name; } public void eat() { System.out.println("Animal is eating."); }}public class Dog extends Animal { public Dog(String name) { super(name); } @Override public void eat() { super.eat(); System.out.println("Dog is also eating."); }}
这段代码展示了Java中的继承和构造方法的使用。Animal类有一个受保护的属性name,以及一个构造方法,用于初始化这个属性。它还有一个公共的eat()方法,输出“Animal is eating。”。
Dog类继承自Animal类,并且也有一个构造方法,用于调用父类的构造方法来初始化name属性。它还重写了父类的eat()方法,在输出“Animal is eating.”之后,再输出“Dog is also eating.”。
这种使用super关键字调用父类构造方法和方法的方式,可以在子类中扩展父类的功能,同时又保留了父类的功能。在这个例子中,Dog类在继承了Animal类的eat()方法之后,还添加了自己的逻辑,输出“Dog is also eating.”,从而扩展了父类的功能。
public class TestPolymorphism { public static void main(String[] args) { show(new Dog()); show(new Cat()); show(new Lion()); } public static void show(Animal a){ if(a instanceof Dog){ Dog c=(Dog) a; c.eat(); }else if(a instanceof Cat){ Cat c=(Cat) a; c.eat(); }else if(a instanceof Lion){ Lion c=(Lion) a; c.eat(); } } abstract class Animal{ abstract void eat(); } class Dog extends Animal{ public void eat(){ System.out.println("eating bread..."); } } class Cat extends Animal{ public void eat(){ System.out.println("eating rat..."); } } class Lion extends Animal{ public void eat(){ System.out.println("eating meat..."); } } }这个代码哪里有问题,怎么改?
这段代码使用了 instanceof 运算符和强制类型转换来实现多态,但是这种做法不够优雅,容易出错,并且增加了代码的复杂性。可以使用方法重载和方法覆盖来实现多态,代码如下所示:
```
public class TestPolymorphism {
public static void main(String[] args) {
Animal animal1 = new Dog();
Animal animal2 = new Cat();
Animal animal3 = new Lion();
show(animal1);
show(animal2);
show(animal3);
}
public static void show(Animal a) {
a.eat();
}
static abstract class Animal {
abstract void eat();
}
static class Dog extends Animal {
public void eat() {
System.out.println("eating bread...");
}
}
static class Cat extends Animal {
public void eat() {
System.out.println("eating rat...");
}
}
static class Lion extends Animal {
public void eat() {
System.out.println("eating meat...");
}
}
}
```
这段代码使用了 Animal 类型的变量来引用不同的动物对象,并且调用了 Animal 类的 eat() 方法,具体的实现由子类来完成。这样可以避免使用 instanceof 和强制类型转换,代码更加简洁清晰。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)