java

Object 클래스

윤만석 2024. 3. 12. 14:34

== 연산자는 참조변수의 참조값, 즉 reference 를 비교한다.

 

class man{
	String name;
	man(String str){
		name=str;
	}
}
public class Main {
	public static void main(String args[]) {
		man m1=new man("minseok");
		man m2=new man("minseok");
		man m3=m1;
		System.out.println(m1==m2);
		System.out.println(m1==m3);
	}
}
//false
//true

따라서, 인스턴스의 내용이 같은지 확인하려면 다른방법을 사용해야한다.

 

Object 클래스의 equal 메소드는 ==와 마찬가지로 레퍼런스를 비교한다. 하지만 equals는 내용을 비교하라고 오버라이딩하라고 존재하는 메소드이다

 

자바에서 많이 사용하는 클래스들은 이미 오버라이딩이 된 경우가 많다 예를들ㅇ ㅓ String 클래스

public class Main {
	public static void main(String args[]) {
		String s1="minseok";
		String s2="minseok";
		//주의할 점 String 클래스를 정의할때 새 인스턴스를 생성하지않고 이런식으로하면 s1==s2이다
		System.out.println(s1==s2);
		System.out.println(s1.equals(s2));
		
		
		String s3=new String("jeonghoon");
		String s4=new String("jeonghoon");
		System.out.println(s3==s4);
		System.out.println(s3.equals(s4));
	}
}
//true
//true
//false
//true

 

 

 

 

 

Object 클래스에는 인스턴스를 복제할 수 있는 Clone 메소드가 있습니다.

단 

interface Clonable //마커 인터페이스 >> 이 클래스의 인스턴스는 복제가능

이 인터페이스를 implement 한 클래스의 인스턴스만 이 clone 메소드를 사용할 수 있습니다.

 

package Devide_And_Conquer;

class point implements Cloneable{
	int x;
	int y;
	point(int x,int y){
		this.x=x;
		this.y=y;
	}
	public void setpoint(int a,int b) {
		this.x=a;
		this.y=b;
	}
	public void print() {
		System.out.println(x);
		System.out.println(y);
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}
class rec implements Cloneable{
	point up;
	point down;
	rec(int a,int b,int c,int d){
		up=new point(a,b);
		down=new point(c,d);
	}
	public void print() {
		up.print();
		down.print();
	}
	public void setRec(int a,int b,int c,int d) {
		up.setpoint(a, b);
		down.setpoint(c, d);
	}
	@Override
	public Object clone() throws CloneNotSupportedException{
		return super.clone();
	}
}
public class Main {
	public static void main(String args[]) {
		rec r1=new rec(1,2,3,4);
		rec r2;
		try {
			r2=(rec)r1.clone();
            //r1을 복제하더라도, r1의 point 인스턴스는 복제되지 않았습니다
            //따라서 r2도 r1의 point인스턴스를 참조합니다
			r1.setRec(5, 6, 7, 8); //r1,r2의 point인스턴스 모두 변합니다.
			r1.print();
			r2.print();
		} catch (CloneNotSupportedException e) {
			
			e.printStackTrace();
		}
		
	}
}
//5 6 7 8
//5 6 7 8
package Devide_And_Conquer;

class point implements Cloneable{
	int x;
	int y;
	point(int x,int y){
		this.x=x;
		this.y=y;
	}
	public void setpoint(int a,int b) {
		this.x=a;
		this.y=b;
	}
	public void print() {
		System.out.println(x);
		System.out.println(y);
	}
	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
}
class rec implements Cloneable{ 
	point up;
	point down;
	rec(int a,int b,int c,int d){
		up=new point(a,b);
		down=new point(c,d);
	}
	public void print() {
		up.print();
		down.print();
	}
	public void setRec(int a,int b,int c,int d) {
		up.setpoint(a, b);
		down.setpoint(c, d);
	}
	@Override
    //따라서 clone메소드를 오버라이딩할때 , point 인스턴스도 clone 해야합니다.
	public Object clone() throws CloneNotSupportedException{
		rec newrec= (rec)super.clone();
		newrec.up=(point)up.clone();
		newrec.down=(point)down.clone();
		return newrec;
	}
}
public class Main {
	public static void main(String args[]) {
		rec r1=new rec(1,2,3,4);
		rec r2;
		try {
			r2=(rec)r1.clone();
			r1.setRec(5, 6, 7, 8);
			r1.print();
			r2.print();
		} catch (CloneNotSupportedException e) {
			
			e.printStackTrace();
		}
		
	}
}

'java' 카테고리의 다른 글

제너릭 Generic  (1) 2024.03.15
Array 사용법  (0) 2024.03.13
interface 정리  (0) 2024.03.11
super ? 상속의 규칙  (0) 2024.03.08
enhanced for  (1) 2024.03.08