zoukankan      html  css  js  c++  java
  • 27 关于Object类 -- equals()

    /*
        关于Object,以其中的eauqls方法为例:
        1.继承Object中的equals方法时,比较的是两个引用是否指向同一个对象
        2.子类可以通过重写equals方法的形式,改变比较的内容
         */
        
        //重写equals方法
        public boolean equals(Object obj){
            if(obj == null)  //必须加判空操作,否则会出现空指针异常
                return false;
            Animal temp = (Animal)obj;  //强制类型转换
            //重新制定equals的判定规则
            if(this.getName().equals(temp.getName()) && (this.getMonth() == temp.getMonth())){
                return true;
            }else{
                return false;
            }
        }
    
        //重载重写后的equals方法
        public boolean equals(Animal obj){
            if(obj == null)  //必须加判空操作,否则会出现空指针异常
                return false;
            if(this.getName().equals(obj.getName()) && (this.getMonth() == obj.getMonth())){
                return true;
            }else{
                return false;
            }
        }

    Object类是所有类的父类,它本身就有一些方法可供所有Java类调用,例如equals方法。

    而且这些方法可以在子类中进行重写和重载,从而执行更具体的功能。

    例:

    Animal.java:

    package com.wys.java.Model;
    
    public class Animal {
        private String name;    //名字
        private int month;      //年龄
        private String species; //品种
    
        //无参构造方法
        public Animal() {
        }
    
        //含参构造方法,对名字、年龄赋值
        public Animal(String name, int month){
            this.name = name;
            this.month = month;
            System.out.println("我是父类Animal类的带参构造方法");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getMonth() {
            return month;
        }
    
        public void setMonth(int month) {
            this.month = month;
        }
    
        public String getSpecies() {
            return species;
        }
    
        public void setSpecies(String species) {
            this.species = species;
        }
    
        /*
        关于Object,以其中的eauqls方法为例:
        1.继承Object中的equals方法时,比较的是两个引用是否指向同一个对象
        2.子类可以通过重写equals方法的形式,改变比较的内容
         */
    
        //重写equals方法
        public boolean equals(Object obj){
            if(obj == null)
                return false;
            Animal temp = (Animal)obj;  //强制类型转换
            //重新制定equals的判定规则
            if(this.getName().equals(temp.getName()) && (this.getMonth() == temp.getMonth())){
                return true;
            }else{
                return false;
            }
        }
    
        //重载重写后的equals方法
        public boolean equals(Animal obj){
            if(obj == null)
                return false;
            if(this.getName().equals(obj.getName()) && (this.getMonth() == obj.getMonth())){
                return true;
            }else{
                return false;
            }
        }
    }
    View Code


    Test.java:

    package com.wys.java.Test;
    
    import com.wys.java.Model.Animal;
    
    public class Test {
        public static void main(String[] args) {
            Animal one = new Animal("花花",2);
            Animal two = new Animal("花花",2);
    
            //关于equals()
            boolean flag = one.equals(two);
            System.out.println("one 和 two 的引用比较:" + flag);
            System.out.println("one 和 two 的引用比较:" + (one == two));
    
            System.out.println("=======================================");
    
            String str1 = new String("nnn");
            String str2 = new String("nnn");
            boolean strFlag = str1.equals(str2);
            System.out.println("str1 和 str2 的引用比较:" + strFlag);
            System.out.println("str1 和 str2 的引用比较:" + (str1 == str2));
        }
    }
    View Code

    结果:

  • 相关阅读:
    Python- 索引 B+数 比如书的目录
    Python-视图 触发器 事务 存储过程
    Python-mysql 权限 pymysql 注入共计
    Shell之Sed常用用法
    python 字符编码讲解
    python enumerate枚举用法总结
    Python第三周 数据类型:集合set、文件的读写、追加操作。
    第二周Python笔记 数据类型 列表 字典
    Python第二周 str的方法
    第二周Python笔记之 变量的三元运算
  • 原文地址:https://www.cnblogs.com/CPU-Easy/p/12169970.html
Copyright © 2011-2022 走看看