Java 34 . Hiểu rõ phương thức so sánh đối tượng equals và phương thức hascode trong Java



    Bản quyền thuộc về TITV.vn, 
    vui lòng không đăng tải lại nội dung từ trang này.

    Video giải thích chi tiết


     Chương trình 

    
        
    public class MyDate {
    	private int day;
    	private int month;
    	private int year;
    	
    	public MyDate(int day, int month, int year) {
    		if (day >= 1 && day <= 31) {
    			this.day = day;
    		} else {
    			this.day = 1;
    		}
    		if (month >= 1 && month <= 12) {
    			this.month = month;
    		} else {
    			this.month = 1;
    		}
    		if (year >= 1) {
    			this.year = year;
    		}else {
    			this.year = 1;
    		}
    	}
    
    	public int getDay() {
    		return day;
    	}
    
    	public void setDay(int day) {
    		if (day >= 1 && day <= 31) {
    			this.day = day;
    		} 
    	}
    
    	public int getMonth() {
    		return month;
    	}
    
    	public void setMonth(int month) {
    		if (month >= 1 && month <= 12) {
    			this.month = month;
    		}
    	}
    
    	public int getYear() {
    		return year;
    	}
    
    	public void setYear(int year) {
    		if (year >= 1) {
    			this.year = year;
    		}
    	}
    
    	@Override
    	public String toString() {
    		return "MyDate [day=" + day + ", month=" + month + ", year=" + year + "]";
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + day;
    		result = prime * result + month;
    		result = prime * result + year;
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (this.getClass() != obj.getClass())
    			return false;
    		MyDate other = (MyDate) obj;
    		if (this.day != other.day)
    			return false;
    		if (this.month != other.month)
    			return false;
    		if (this.year != other.year)
    			return false;
    		return true;
    	}
    
    //	@Override
    //	public String toString() {
    //		return this.day + "/" + this.month + "/"+this.year;
    //	}
    	
    	
    }
        
    
    
    public class Test {
    	public static void main(String[] args) {
    		MyDate md1 = new MyDate(15, 5, 2025);
    		MyDate md2 = new MyDate(11, 1, 2021);
    		MyDate md3 = new MyDate(15, 5, 2025);
    		
    		System.out.println(md1); // 15/5/2021
    		System.out.println(md2);
    		System.out.println(md3);
    		
    //		int a = 5;
    //		int b = 6;
    //		if(a==b) {
    //			System.out.println("a = b");
    //		}else {
    //			System.out.println("a != b");
    //		}
    		
    //		if(md1==md3) {
    //			System.out.println("md1 == md3");
    //		}else {
    //			System.out.println("md1 != md3");
    //		}
    		
    		System.out.println("md1 so sanh bang md2: "
    		                        + md1.equals(md2));
    		
    		System.out.println("md1 so sanh bang md3: "
                                    + md1.equals(md3));
    		
    		System.out.println("Hashcode md1 " + md1.hashCode());
    		System.out.println("Hashcode md2 " + md2.hashCode());
    		System.out.println("Hashcode md3 " + md3.hashCode());
    	}
    }
    

    
    public class Test {
    	public static void main(String[] args) {
    		MyDate md1 = new MyDate(15, 5, 2025);
    		MyDate md2 = new MyDate(11, 1, 2021);
    		MyDate md3 = new MyDate(15, 5, 2025);
    		
    		System.out.println(md1); // 15/5/2021
    		System.out.println(md2);
    		System.out.println(md3);
    		
    //		int a = 5;
    //		int b = 6;
    //		if(a==b) {
    //			System.out.println("a = b");
    //		}else {
    //			System.out.println("a != b");
    //		}
    		
    //		if(md1==md3) {
    //			System.out.println("md1 == md3");
    //		}else {
    //			System.out.println("md1 != md3");
    //		}
    		
    		System.out.println("md1 so sanh bang md2: "
    		                        + md1.equals(md2));
    		
    		System.out.println("md1 so sanh bang md3: "
                                    + md1.equals(md3));
    		
    		System.out.println("Hashcode md1 " + md1.hashCode());
    		System.out.println("Hashcode md2 " + md2.hashCode());
    		System.out.println("Hashcode md3 " + md3.hashCode());
    	}
    }
    
    
    
         

    Bạn có thể thích những bài đăng này:

    1 nhận xét:

    1. Bài giảng rất hay ạ! Xem tới đâu, hiểu đến đó. Rất cảm ơn tác giả.

      Trả lờiXóa