随笔 - 322  文章 - 0  评论 - 4  阅读 - 77146

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   Bytezero!  阅读(66)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示