美文网首页
对象比较

对象比较

作者: 落在牛背上的鸟 | 来源:发表于2018-03-14 07:14 被阅读14次
    1. 关于对象比较的实现模式;
    2. this在对象比较中的应用。

    问题: 如果一个自定义类,要想判断它的两个对象是否相等,那么必须要实现类对象之中所有属性内容的比较。
    范例:基础的比较方式

    class Book {
        private String title;
        private int price;
        public Book(String title , int price) {
            this.title = title;
            this.price = price;
        }
        public String getTitle() {
            return this.title;
        }
        public int getPrice() {
            return this.price;
        }
    }
    
    
    public class TestCompare {
        public static void main(String[] args) {
            Book b1 = new Book("Java开发" , 20);
            Book b2 = new Book("Java开发" , 20);
            if (b1.getPrice() == b2.getPrice() && b1.getTitle().equals(b2.getTitle())) {
                System.out.println("True");
            }else {
                System.out.println("false");
            }
        }
    }
    

    此时一个最基础的对象比较的功能实现,但是以上的程序里面存在有问题:
    主方法(main())就是一个客户端,客户端的程序逻辑越简单越好,最好隐藏所有的细节逻辑。
    如果对象要进行信息比较,必须有每个对象自己完成,对象所在的类一定要提供有对象比较的方法。
    额外内容:
    如果类中的属性使用了private封装,那么类的外部就不能通过对象直接调用属性。
    但是将一个对象传递回到类的方法里面,就相当于取消了封装的形式,可以直接通过对象访问属性。

    class Info {
        private String msg = "Hello";
        public void println() {
            System.out.println("msg =" + msg );
        }
    
        public void fun(Info info) {
            info.msg = "World";
        }
    }
    
    public class Demo {
        public static void main(String[] args) {
            Info x = new Info();
            x.fun(x);   //没有实际意义,只是一个语法验证
            x.println();
        }
    }
    

    此类型的代码会在对象比较中出现,但是也是一种常见形式,即:一个类可以接受本类对象。

    范例:对象比较实现

    class Book {
        private String title;
        private int price;
        public Book(String title , int price) {
            this.title = title;
            this.price = price;
        }
        //本类接受本类对象,对象可以直接访问属性,不需要使用getter方法
        //两个功能:带回了需要的比较信息;方便访问
        public boolean compare(Book book) {
            if (book == null || this == book) {
                //如果为空,和 如果是对象本身,直接返回false  
                return false;   
            }
            //
            if(this.title.equals(book.title) 
                && this.price == book.price) {
                return true;
            }else{
                return false;
            }
    
        }
        public String getTitle() {
            return this.title;
        }
        public int getPrice() {
            return this.price;
        }
    }
    
    
    public class TestCompare {
        public static void main(String[] args) {
            Book b1 = new Book("Java开发" , 20);
            Book b2 = new Book("Java开发" , 20);
            if (b1.compare(b2)) {
                System.out.println("True");
            }else {
                System.out.println("false");
            }
        }
    }
    

    对象比较的操作代码形式都是固定的,都会按照固定的步骤进行同一对象的验证,并且在对象比较里面也可以发现this表示当前对象。

    总结

    1. 对象比较一定是某一个类自己定义的功能;
    2. 对象比较时一定要判断是否为null、地址是否相同、属性是否相同。

    相关文章

      网友评论

          本文标题:对象比较

          本文链接:https://www.haomeiwen.com/subject/vcxgfftx.html