Java 多态性的使用

 1 package com.bytezreo.duotai3;
 2 
 3 /***
 4  * 
 5  * @Description  练习多态性的使用
 6  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 7  * @version
 8  * @date 2021年9月24日上午8:46:24
 9  * @     
10  * 编写 equalsArea 方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术)
11  * 编写 displayGeometricObject方法显示对象的面积(注意方法的参数类型,利用动态绑定技术)
12  * 
13  *
14  */
15 public class GeometricTest
16 {
17     public static void main(String[] args)
18     {
19         GeometricTest test = new GeometricTest();
20     
21         Circle c1 = new Circle(2.3,"black",5.0);
22         test.displayGeometricObject(c1);
23         
24         Circle c2 = new Circle(4.3,"red",5.0);
25         test.displayGeometricObject(c2);
26          boolean isEquals = test.equalsArea(c1, c2);
27          System.out.println("c1和c2的面积是否相等:" + isEquals);
28         
29         Circle c3 = new Circle(2.3,"black",5.0);
30         test.displayGeometricObject(c3);
31         
32         boolean isEquals1 = test.equalsArea(c1, c3);
33         System.out.println("c1和c3的面积是否相等:" + isEquals1);
34         
35         MyRectangle r1  = new MyRectangle(2.2,3.3,"red",1.0);
36         test.displayGeometricObject(r1);
37         
38         
39         
40     }
41     
42     
43     //GeometricObject o  = new Circle(...);
44     public void displayGeometricObject(GeometricObject o)
45     {
46         System.out.println("面积为:"+ o.findAre());
47     }
48     
49     
50     
51     //测试两个对象的面积是否相等
52     public boolean equalsArea(GeometricObject o1,GeometricObject o2)
53     {
54          return o1.findAre() == o2.findAre();
55     }
56     
57     
58     
59     
60 }
 1 package com.bytezreo.duotai3;
 2 
 3 public class GeometricObject    //几何图形
 4 {
 5     protected  String color;
 6     protected  double weight;
 7     public String getColor() {
 8         return color;
 9     }
10     public void setColor(String color) {
11         this.color = color;
12     }
13     public double getWeight() {
14         return weight;
15     }
16     public void setWeight(double weight) {
17         this.weight = weight;
18     }
19 //    public GeometricObject()
20 //    {
21 //        
22 //    }
23     public GeometricObject(String color, double weight) {
24         super();
25         this.color = color;
26         this.weight = weight;
27     }
28     
29     
30     public double findAre()
31     {
32        return 0.0;    
33     }
34     
35 }
 1 package com.bytezreo.duotai3;
 2 
 3 public class Circle extends GeometricObject
 4 {
 5     
 6     private double radius;
 7 
 8     public Circle(double radius,String color, double weight) 
 9     {
10         super(color, weight);
11         
12         this.radius = radius;
13         
14     }
15 
16     public double getRadius() {
17         return radius;
18     }
19 
20     public void setRadius(double radius) {
21         this.radius = radius;
22     }
23     
24     @Override
25     public double findAre()
26     {
27         // TODO Auto-generated method stub
28         return 3.14 *radius *radius;
29     }
30     
31 
32     
33     
34     
35     
36     
37     
38 }
 1 package com.bytezreo.duotai3;
 2 
 3 public class MyRectangle extends GeometricObject
 4 {
 5     private double width;
 6     private double height;
 7 
 8     public MyRectangle( double width, double height,String color, double weight)
 9     {
10         super(color, weight);
11         this.width = width;
12         this.height = height;
13         
14         
15         
16     }
17 
18     public double getWidth() {
19         return width;
20     }
21 
22     public void setWidth(double width) {
23         this.width = width;
24     }
25 
26     public double getHeight() {
27         return height;
28     }
29 
30     public void setHeight(double height) {
31         this.height = height;
32     }
33     
34     
35     @Override
36     public double findAre() 
37     {
38         
39         return width * height;
40     }
41     
42     
43 
44 }

 

posted on 2021-09-24 09:12  Bytezero!  阅读(41)  评论(0编辑  收藏  举报