Java面向对象抽象类实例练习

时间:2022-05-07
本文章向大家介绍Java面向对象抽象类实例练习,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 abstract class Animal
 2 {
 3     abstract void eat();
 4 }
 5 
 6 class Cat extends Animal
 7 {
 8     void eat()
 9     {
10         System.out.println("eat fish");
11     }
12 }
13 
14 class Dog extends Animal
15 {
16     void eat()
17     {
18         System.out.println("gnaw bone");
19     }
20 }
21 
22 class Pig extends Animal
23 {
24     void eat()
25     {
26         System.out.println("eat rice");
27     }
28 }
29 
30 class Duotai
31 {
32     public static void main(String[] args)
33     {
34         method(new Cat());
35         method(new Dog());
36         method(new Pig());
37     }
38     public static void method(Animal a)
39     {
40         a.eat();
41     }
42 }