package demo2;
public class Contact {
private int no;
private String name;
private String tel;
private String email;
public Contact() {}
public Contact(int no, String name, String tel, String email) {
super();
this.no = no;
this.name = name;
this.tel = tel;
this.email = email;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// 오브젝트의 toString[] 메소드 재정의
@Override
public String toString() {
return "Contact [no=" + no + ", name=" + name + ", tel=" + tel + ", email=" + email + "]";
}
public Contact copyContact() throws CloneNotSupportedException {
Object obj = clone();
Contact c = (Contact)obj;
return c;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + no;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Contact other = (Contact) obj;
if (no != other.no)
return false;
return true;
}
}
package demo2;
public class ContactApp {
public static void main(String[] args) {
Contact c = new Contact(10, "김유신", "010-1111-1111", "kim@naver.com");
Contact cc = new Contact(10, "김유신", "010-1111-1111", "kim@naver.com");
//int hashcode() : 객체에 부여된 일련번호를 점수로 제공하는 메소드
int hash = c.hashCode();
int hash2 = cc.hashCode();
System.out.println("해쉬코드 값 : " + hash);
System.out.println("해쉬코드 값 : " + hash2);
// String toString() : 객체의 정보를 문자열로 제공하는 메소드
// 설계도의 이름 + "@" + 해시코드값(16진수)
// toString()메소드는 재정의해서 필드에 저장된 값들을 문자열로 이어서 제공하도록
// 재정의해서 사용한다.
String info = c.toString();
String info2 = cc.toString();
System.out.println("객체의 정보 : " + info);
System.out.println("객체의 정보 : " + info2);
//System.out.println(c);
//System.out.println(cc);
//System.out.println(c.toString());
//System.out.println(cc.toString());
// boolean equals(Object other) : - 전달받은 다른 객체와 이 객체가 동일한 객체인지 여부를 반환한다.
boolean res1 = c.equals(cc);
boolean res2 = cc.equals(c);
System.out.println(res1);
System.out.println(res2);
// 같은 곳을 바라볼 때만(=동일한 객체일 때만) true
}
}
package demo2;
public class ContactCloneApp {
public static void main(String[] args) throws Exception {
Contact src = new Contact(100, "홍길동", "010-1111-1111", "hong@gmail.com");
Contact cloneContact = src.copyContact();
System.out.println(cloneContact.getNo());
System.out.println(cloneContact.getName());
System.out.println(cloneContact.getTel());
System.out.println(cloneContact.getEmail());
}
}
package demo2;
import java.lang.reflect.Method;
import org.omg.CORBA.Context;
public class ContactReflect {
public static void main(String[] args) throws Exception {
Contact c = new Contact(10, "홍길동", "010-1111-2222", "hong@hanmail.com");
Class<?> clazz = c.getClass();
Method[] methods = clazz.getMethods();
for (Method m : methods) {
System.out.println(m.getName());
}
}
}