
- 클래스 Person은 이름을 저장하는 필드 구성
- 클래스 Person은 상위 클래스 Object의 메소드 equals()를 오버라이딩하여
이름이 같으면 true를 반환하는 메소드 구현
- 다음과 같은 소스로 클래스 Person을 점검
Person p1 = new Person("홍길동");
System.out.println(p1.equals(new Person("홍길동")));
System.out.println(p1.equals(new Person("최명태")));
class PersonE{
String name;
public PersonE(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof PersonE) {
if(this.name == ((PersonE)obj).name)
return true;
}
return false;
}
}
public class PersonTest {
public static void main(String[] args) {
PersonE p1 = new PersonE("홍길동");
System.out.println(p1.equals(new PersonE("홍길동")));
System.out.println(p1.equals(new PersonE("최명태")));
}
}
public static void main(String[] args) {
String s1 = new String("java");
String s2 = new String("java");
String s3 = s2;
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(s2 == s3);
System.out.println(s2.equals(s3));
if(s1 == s2)
System.out.println("s1, s2 참조 대상 같다.");
else
System.out.println("s1, s2 참조 대상 다르다. ");
if(s2 == s3)
System.out.println("s2, s3 참조 대상 같다.");
else
System.out.println("s2, s3 참조 대상 다르다. ");
if(s1.equals(s2))
System.out.println("s1, s2 내용 동일하다.");
else
System.out.println("s1, s2 내용 다르다.");
if(s2.equals(s3))
System.out.println("s2, s3 내용 동일하다.");
else
System.out.println("s2, s3 내용 다르다.");
== 연산: 주소값 비교
equals: 문자열의 내용 비교
new로 객체 생성을 하면 참조 주소가 저장된다.
== 연산시에는 주소값을 비교하게 된다.
참조 주소가 아닌 문자열의 내용 비교를 위해서는 equals를 오버라이드 해서 사용해야 하지만 이미 String클래스에 equals가 오버라이딩 되어있으므로 그냥 사용하면 된다.
INum[] ar1 = new INum[3];
INum[] ar2 = new INum[3];
ar1[0] = new INum(1); ar2[0] = new INum(1);
ar1[1] = new INum(2); ar2[1] = new INum(2);
ar1[2] = new INum(3); ar2[2] = new INum(3);
System.out.println(Arrays.equals(ar1, ar2));
답
import java.util.Arrays;
class INum2{
int num;
public INum2(int num) {
this.num = num;
}
@Override
public boolean equals(Object obj) {
if(this.num == ((INum2)obj).num) {
return true;
}
return false;
}
}
public class Equals2 {
public static void main(String[] args) {
INum[] ar1 = new INum[3];
INum[] ar2 = new INum[3];
ar1[0] = new INum(1);
ar2[0] = new INum(1);
ar1[1] = new INum(2);
ar2[1] = new INum(2);
ar1[2] = new INum(3);
ar2[2] = new INum(3);
System.out.println(Arrays.equals(ar1, ar2));
}
}
클래스나 메소드에서 사용할 내부 데이터 타입을 컴파일 시에 미리 지정하는 방법
<선언>
class MyArray<T> {
T element;
void setElement(T element) { this.element = element; }
T getElement() { return element; }
}
<생성>
MyArray<Integer> myArr = new MyArray<Integer>();
public static void main(String[] args) {
Box aBox = new Box();
Box oBox = new Box();
// 아래 두 문장에서는 사과와 오렌지가 아닌 '문자열'을 담았다.
aBox.set("Apple");
oBox.set("Orange");
// 상자에 과일이 담기지 않았는데 과일을 꺼내려 한다.
Apple ap = (Apple)aBox.get();
Orange og = (Orange)oBox.get();
System.out.println(ap);
System.out.println(og);
}
public static void main(String[] args) {
Box aBox = new Box();
Box oBox = new Box();
// 다음 두 문장은 프로그래머의 실수이다!
aBox.set("Apple");
oBox.set("Orange");
System.out.println(aBox.get());
System.out.println(oBox.get());
}
public static void main(String[] args) {
DBox<String, Integer> box = new DBox<String, Integer>();
box.set("Apple", 25);
System.out.println(box);
}
답
class DBox<T1,T2>{
private T1 ob1;
private T2 ob2;
public void set(T1 ob1, T2 ob2) {
this.ob1 = ob1;
this.ob2 = ob2;
}
@Override
public String toString() {
return ob1 + " " + ob2;
}
}
public class DBoxMain {
public static void main(String[] args) {
DBox<String, Integer> box = new DBox<String, Integer>();
box.set("Apple", 25);
System.out.println(box);
}
}