zoukankan      html  css  js  c++  java
  • 【Effective Java】4、覆盖equals时请遵守通用约定

    package cn.xf.cp.ch02.item8.transitivity;
    
    public class Point
    {
        private final int x;
        private final int y;
        
        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        
        @Override
        public boolean equals(Object obj)
        {
            if(!(obj instanceof Point))
            {
                return false;
            }
            Point p = (Point) obj;
            return this.x == p.x && this.y == p.y;
        }
    }

    首先写一个点类

    然后是一个颜色的,枚举

    package cn.xf.cp.ch02.item8.transitivity;
    
    public enum Color
    {
        RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
    }

    开始重写其equals方法,来综合比较颜色和点的属性

    package cn.xf.cp.ch02.item8.transitivity;
    
    public class ColorPoint extends Point
    {
        private final Color color;
        public ColorPoint(int x, int y, Color color)
        {
            super(x, y);
            this.color = color;
        }
    
        //这里写个euqals,来比较颜色点,但是这样的话普通点:颜色点 和 颜色点 :普通点返回不一样
        public boolean equals1(Object obj)
        {
            if(!(obj instanceof ColorPoint))
            {
                return false;
            }
            //这样进行比较,保证父类和自己
            return super.equals(obj) && ((ColorPoint) obj).color == color;
        }
        
        //但是上面方法不能区别(1,2)的点和(1,2,red)这样比较的时候,如果我们不想比较颜色的时候
        //所以改进,保证x.equals(y)和y.equals(x)保持一致
        //但是这样,x.equals(y)和y.equals(z),x.equals(z)
        public boolean equals2(Object obj)
        {
            if(!(obj instanceof Point))
            {
                return false;
            }
            
            if(!(obj instanceof ColorPoint))
            {
                //如果是Point对象不是颜色点对象,那就直接比x,y的大小
                return obj.equals(this);
            }
            //比较全部内容
            return super.equals(obj) && ((ColorPoint) obj).color == color;
        }
    }

    在实现这样的比较的时候

    使用复合优先于继承

    我们使用复合的方式来做就会方便很多

    package cn.xf.cp.ch02.item8.transitivity;
    
    public class ColorPoint2
    {
        private final Point point;
        private final Color color;
        
        public ColorPoint2(int x, int y, Color color)
        {
            if(color == null)
                throw new NullPointerException();
            point = new Point(x, y);
            this.color = color;
        }
        
        public Point asPoint()
        {
            return point;
        }
        
        @Override
        public boolean equals(Object obj)
        {
            if(!(obj instanceof ColorPoint2))
                return false;
            
            ColorPoint2 cp2 = (ColorPoint2) obj;
            //分别调用本书的equals方法进行比较
            return cp2.point.equals(point) && cp2.color.equals(color);
        }
        
        
    }
  • 相关阅读:
    移动端 css 禁止长按屏幕选中
    找到并替换 字符串中最后一个(不一定是末尾最后一个) 指定字符
    event.preventDefault() 解决按钮多次点击 导致页面变大
    history.go(-1)在不同浏览器中的解析
    clean-css 安装 使用
    Objective-C通过联合存储为类增加属性及原理解析
    IOS-CGAffineTransformMake 矩阵变换 的运算原理
    iOS--inputView和inputAccessoryView
    Objective-C中的@Property详解
    Objective-C--Runtime机制
  • 原文地址:https://www.cnblogs.com/cutter-point/p/5854670.html
Copyright © 2011-2022 走看看