编写一个应用抽象类的程序要求设计抽象类设计继承抽象类并实现抽象类中抽象方法的子类不用图形类
时间: 2023-12-06 07:34:54 浏览: 129
根据提供的引用内容,抽象类是一种不能被实例化的类,它的主要作用是为子类提供一个公共的基础结构,子类可以继承抽象类并实现其中的抽象方法。下面是一个应用抽象类的程序设计:
```java
// 定义一个抽象类Animal
abstract class Animal {
// 定义一个抽象的捕食方法
public abstract void hunt();
// 定义一个非抽象的进食方法,在进食方法中调用捕食方法
public void eat() {
System.out.println("Animal is eating...");
hunt();
}
}
// 定义一个子类Tiger,继承自Animal
class Tiger extends Animal {
// 实现抽象方法hunt
public void hunt() {
System.out.println("Tiger is hunting...");
}
}
// 定义一个子类Lion,继承自Animal
class Lion extends Animal {
// 实现抽象方法hunt
public void hunt() {
System.out.println("Lion is hunting...");
}
}
// 测试程序
public class Test {
public static void main(String[] args) {
// 创建一个Tiger对象
Tiger tiger = new Tiger();
// 调用eat方法
tiger.eat();
// 创建一个Lion对象
Lion lion = new Lion();
// 调用eat方法
lion.eat();
}
}
```
在上面的程序中,我们定义了一个抽象类Animal,其中包含一个抽象方法hunt和一个非抽象方法eat。然后我们定义了两个子类Tiger和Lion,分别继承自Animal并实现其中的抽象方法hunt。最后我们在测试程序中创建了一个Tiger对象和一个Lion对象,并分别调用它们的eat方法。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![class](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)