equals方法与==的区别


 1 public class Demo3 {
 2     public static void main(String[] args) {
 3  Son son=new Son();
 4  Son son1=new Son(1.0);
 5  Son son2=new Son(1.0,"white",4);
 6         System.out.println(son1.color.equals(son2.color));
 7         System.out.println(son2.toString()+"\n"+son1.toString());
 8         System.out.println(son1.equals(son2));
 9 
10 
11     }
12 
13 
14 
15 
16 }
17 
18 class Father{
19     protected   String color;
20     protected   double weight;
21 
22    protected Father(){
23        this.color="white";
24        this.weight=1.0;
25    }
26    protected  Father(String color,double weight){
27        this.color=color;
28        this.weight=weight;
29    }
30 
31     public String getColor() {
32         return color;
33     }
34 
35     public void setColor(String color) {
36         this.color = color;
37     }
38 
39     public double getWeight() {
40         return weight;
41     }
42 
43     public void setWeight(double weight) {
44         this.weight = weight;
45     }
46 }
47 
48 class  Son extends Father{
49     private double radius;
50     public  Son(){
51         this.radius=1.0;
52         super.color="white";
53         super.weight=1.0;
54     }
55     public  Son(double radius){
56         this.color="white";
57         this.weight=1.0;
58         this.radius=radius;
59 
60     }
61     public Son(double radius,String color,double weight){
62         super(color,weight);
63         this.radius=radius;
64 
65 
66     }
67 
68     public double getRadius() {
69         return radius;
70     }
71 
72     public void setRadius(double radius) {
73         this.radius = radius;
74     }
75     public double findArea(){//圆的面积
76         return Math.PI*radius*radius;
77     }
78 
79     @Override
80     public boolean equals(Object o) {
81         if (this==o){
82             return true;
83         }if (o instanceof Son){
84             Son son=(Son) o;//强制类型转换,才可以调用Son类中的属性
85             return this.radius==son.radius;
86         }return false;
87     }
88 
89     @Override
90     public String toString() {
91         return "Son{" + "radius=" + radius + '}';
92     }
93 }

 

 1 public class Demo3 {
 2     
 3     /**
 4      * 1:== 运算符可以在基本类型与引用类型之间使用
 5      * 基本类型比较的是数据值,引用类型比较的是地址值(即两个引用是否指向同一个对象)
 6      *
 7      *2:equals()方法的使用
 8      *  是一个方法,不是运算符
 9      *  不适用于基本数据类型,只适用于引用数据类型
10      *  Object类中equals()的定义为
11      *  public boolean equals(Object obj) {
12      *         return (this == obj);
13      *     }
14      *与==运算符作用相同,比较两个对象的地址值(引用类型)
15      *像String,Date,File,包装类等都重写了object类中的equals()方法(是因为现实开发中通常只关
16      * 两个实体内容数据是否相等,不怎么关心是否是同一个引用对象)
17      *
18      *
19      *
20      */
21 
22     public static void main(String[] args) {
23          Demo3 demo3=new Demo3();
24          Demo3 demo31=new Demo3();
25 String str1="huang";
26 String str2="hpp";
27         System.out.println(str1.equals(str2));//String类重写了equals方法,比较的是数据值
28         System.out.println(demo31.equals(demo3));//比较的是地址值
29         
30 
31 
32 
33     }
34 
35 
36 }

 

posted @ 2021-09-17 14:32  tiiiiii  阅读(30)  评论(0)    收藏  举报