• 重写equals方法


    /*
    Object类是所有对象的父类,最高级
    该类中定义的是所有类都具有的功能;
    1、方法equals比较的是对象的内存地址。
    */
    
    class ObjectDemo
    {
    	private int num;
    	public  boolean equals(Object obj)                   //重写Object的equals方法
    	{
    		if((obj instanceof ObjectDemo))
    		{
    			ObjectDemo od=(ObjectDemo)obj;
    			return this.num==od.num;
    		}		
    		else
    		{
    			return false;
    		}
    	}
    }
    
    class  EqualsDemo
    {
    	public static void main(String[] args) 
    	{
    		ObjectDemo od1=new ObjectDemo();
    		ObjectDemo od2=new ObjectDemo();
    		ObjectDemo od3=od1;
    		System.out.println(od1.equals(od2));
    		System.out.println(od1==od2);					//比较地址
    		System.out.println(od1.equals(od3));
    <span style="white-space:pre">		System.out.println(od1.toString());				</span>//转换为字符串<span style="white-space:pre">				
    <span style="white-space:pre">		</span>System.out.println(Integer.toHexString(od1.hashCode()));	</span>//获取对象的哈希值
    		/*			
    --------- 运行 ----------
    true
    false
    true
    ObjectDemo@16dadf9
    16dadf9
    输出完成 (耗时 0 秒) - 正常终止
    		*/
    	}
    }
    

  • 相关阅读:
    DNS知识点
    云服务镜像模板配置
    CoreDNS域名解析问题
    ingress配置静态资源
    PAGE_SIZE THREAD_SIZE STACKSIZE
    dockerregistry
    内联视图无法发生视图合并的几种情况
    oracle列删除
    ash_top_plsql
    oracle json()函数put优化案例
  • 原文地址:https://www.cnblogs.com/dengshiwei/p/4258562.html
Copyright © 2020-2023  润新知