|--需求说明
Java 设计鸟类Bird 鱼类Fish 都继承自抽象的动物类Animal, 实现其抽象方法Info 输出各自信息
|--实现思路
|--代码内容
1 /**
2 * @auther::9527
3 * @Description: 动物类
4 * @program: Port
5 * @create: 2019-07-17 22:22
6 */
7 public abstract class Animal {
8 private String color;
9 private String type;
10 private int age;
11 private int weight;
12
13 public String getColor() {
14 return color;
15 }
16
17 public void setColor(String color) {
18 this.color = color;
19 }
20
21 public String getType() {
22 return type;
23 }
24
25 public void setType(String type) {
26 this.type = type;
27 }
28
29 public int getAge() {
30 return age;
31 }
32
33 public void setAge(int age) {
34 this.age = age;
35 }
36
37 public int getWeight() {
38 return weight;
39 }
40
41 public void setWeight(int weight) {
42 this.weight = weight;
43 }
44
45 public abstract void info();
46 }
抽象类---动物类
1 /**
2 * @auther::9527
3 * @Description:
4 * @program: Port
5 * @create: 2019-07-17 22:32
6 */
7 public class Fish extends Animal {
8 @Override
9 public String getColor() {
10 return super.getColor();
11 }
12
13 @Override
14 public void setColor(String color) {
15 super.setColor(color);
16 }
17
18 @Override
19 public String getType() {
20 return super.getType();
21 }
22
23 @Override
24 public void setType(String type) {
25 super.setType(type);
26 }
27
28 @Override
29 public int getAge() {
30 return super.getAge();
31 }
32
33 @Override
34 public void setAge(int age) {
35 super.setAge(age);
36 }
37
38 @Override
39 public int getWeight() {
40 return super.getWeight();
41 }
42
43 @Override
44 public void setWeight(int weight) {
45 super.setWeight(weight);
46 }
47
48 @Override
49 public void info() {
50 System.out.println("我是一条"+getWeight()+"重的"+getType());
51 System.out.println("今年"+getAge()+"岁了");
52 }
53 }
具体类--鱼类
1 /**
2 * @auther::9527
3 * @Description: 鸟类
4 * @program: Port
5 * @create: 2019-07-17 22:23
6 */
7 public class Bird extends Animal {
8
9 @Override
10 public String getColor() {
11 return super.getColor();
12 }
13
14 @Override
15 public void setColor(String color) {
16 super.setColor(color);
17 }
18
19 @Override
20 public String getType() {
21 return super.getType();
22 }
23
24 @Override
25 public void setType(String type) {
26 super.setType(type);
27 }
28
29 @Override
30 public int getAge() {
31 return super.getAge();
32 }
33
34 @Override
35 public void setAge(int age) {
36 super.setAge(age);
37 }
38
39 @Override
40 public int getWeight() {
41 return super.getWeight();
42 }
43
44 @Override
45 public void setWeight(int weight) {
46 super.setWeight(weight);
47 }
48
49 @Override
50 public void info() {
51 System.out.println("我是一只"+getColor()+"的"+getType());
52 System.out.println("今年"+getAge()+"岁了");
53 }
54 }
具体类--鸟类
1 /**
2 * @auther::9527
3 * @Description: 测试类
4 * @program: Port
5 * @create: 2019-07-17 22:34
6 */
7 public class Test {
8 public static void main(String[] args) {
9 //父类引用指向子类对象,把鸟类的对象赋值到父类Animal上面
10 Animal animal = new Bird();
11 animal.setColor("红色");
12 animal.setType("鸟");
13 animal.setAge(4);
14 animal.info();
15
16 System.out.println("\n****---****---****\n");
17
18 //父类引用指向子类对象,把鱼类的对象赋值到父类Animal上面
19 Animal animal1 = new Fish();
20 animal1.setWeight(5);
21 animal1.setType("鱼");
22 animal1.setAge(2);
23 animal1.info();
24
25 }
26 }
测试类
|--运行结果