티스토리 뷰

day17 190314


이번 시간부터 당분간은 자바의 API에 대해서 다루게 될 예정입니다.

API란, 개발에 자주 사용되는 클래스 및 인터페이스의 모음을 의미합니다.

http://docs.oracle.com/javase/8/docs/api/    << 좌측 링크로 들어가시면 oracle.com사에서 제공하는 자바 버전별 지원API에 대해 자세하게 설명이 되어 있습니다. 사용하시면서 언제든지 보고 어떤메소드인지, 어떻게 써야하는지 참고하시면서 사용하시면 되겠습니다. (영어입니다..)


API 설명은 소스안에 바로바로 주석에 설명을 적어놓았으니, 주석을 봐주시기 바랍니다.


<Object API>   (java.lang.Object)

: Object는 모든 클래스의 부모클래스로, 모든 객체에 담을 수 있다.

 ( => 모든 객체가 Object의 API 사용가능)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package demo2;
 
public class Contact implements Cloneable {
    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;
    }
 
    
    // Object의 toString() 메소드 재정의
    @Override
    public String toString() {
        return "Contact [no=" + no + ", name=" + name + ", tel=" + tel + ", email=" + email + "]";
    }
 
    @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;
    }
    
    public Contact copyContact() throws CloneNotSupportedException {
        Object obj = clone();
        Contact c = (Contact) obj;
        return c;
    }
    
}
 
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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");
//        Contact cc = new Contact(20, "강감찬", "010-2222-2222", "kang@ndaum.net");
        
        
        // 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);
        // c값만 출력해도 c.toString()의 실행결과가 출력된다.
        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);
    }
}
 
cs

>> Object API에는 주요 API인, 10행 hashCode,  16행 toString, 30행 equals가 있다.

그 외, 

-finalize() : 맨 마지막으로 한번 실행됨.

-getClass() : 객체의 설계도 정보(final).

-(equals와 hashCode와 같이 재정의하여 사용됨)




댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/10   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함