Java 重写 equals + toString 练习

 1 package com.bytezreo.objectclass2;
 2 
 3 /**
 4  * 
 5  * @Description   重写 equals  +  toString
 6  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 7  * @version
 8  * @date 2021年9月24日下午3:09:52
 9  * @
10  *
11  */
12 public class CircleTest {
13     public static void main(String[] args) {
14         
15         Circle circle1 = new Circle(2.3);
16         Circle circle2 = new Circle(2.31,"while",2.0);
17         
18         
19         System.out.println("颜色是否相等:"+circle1.getColor().equals(circle2.getColor()));
20         
21         System.out.println("半径是否相等:"+circle1.equals(circle2));
22         
23         System.out.println(circle1);
24         System.out.println(circle2.toString());
25     
26     }
27 
28 }
 1 package com.bytezreo.objectclass2;
 2 
 3 
 4 
 5 public class Circle extends GeometricObject {
 6     
 7     private double radius;
 8     
 9     public Circle()
10     {
11         super();
12         radius = 1.0;
13         
14 //        color = "while";
15 //        weight = 1.0;
16         
17     }
18 
19     public Circle(double radius) {
20         super();
21         this.radius = radius;
22     }
23     
24     
25     public Circle(double radius,String  color,double weight) {
26         super(color,weight);
27         this.radius = radius;
28     }
29 
30     public double getRadius() {
31         return radius;
32     }
33 
34     public void setRadius(double radius) {
35         this.radius = radius;
36     }
37     
38     //求圆的面积
39     public double findAre() {
40         return 3.14* radius *radius;
41     }
42     
43     //比较半径是否相等
44     @Override
45     public boolean equals(Object obj) {
46         
47         if(this == obj) {
48             return true;
49         }
50         
51         if(obj instanceof Circle) {
52             Circle c1 = (Circle)obj;
53             return this.radius == c1.getRadius();
54         }
55         
56         return false;
57         
58     }
59 
60     @Override
61     public String toString() {
62         return "Cirlcle [radius=" + radius + "]";
63     }
64 
65     
66     
67 }
 1 package com.bytezreo.objectclass2;
 2 
 3 public class GeometricObject {
 4 
 5     
 6     protected  String color;
 7     protected  double weight;
 8     
 9     
10     
11     public GeometricObject() {
12          super();
13          this.color = "while";
14          this.weight = 1.0;
15     }
16 
17 
18 
19     public GeometricObject(String color, double weight) {
20         super();
21         this.color = color;
22         this.weight = weight;
23     }
24 
25 
26 
27     public String getColor() {
28         return color;
29     }
30 
31 
32 
33     public void setColor(String color) {
34         this.color = color;
35     }
36 
37 
38 
39     public double getWeight() {
40         return weight;
41     }
42 
43 
44 
45     public void setWeight(double weight) {
46         this.weight = weight;
47     }
48     
49     
50     
51     
52 }

 

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