Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

개발 한번 해블로그

[Java] 22. 자바 최상위 포식자 클래스 Object 본문

Java

[Java] 22. 자바 최상위 포식자 클래스 Object

hide on bush 2023. 12. 20. 11:59

1. Object 클래스를 이용하여 객체 생성하기

어떤 클래스를 한 개를 만들면 기본적으로 생략된 코드

1) default package : import java.lang*;
2) 최상위 클래스 : java.lang.Object
3) default 생성자 : public A(){ super();}
- 클래스 이름과 메서드 동일
- return 타입 쓰면 안됨
- 생성자안에 super(); 생략


2. Object 클래스를 활용한 다형성 적용

1) 다형성인수

public class ObjectPoly_04 {
    public static void main(String[] args) {
        A_04 a=new A_04();
        display(a);
        B_04 b=new B_04();
        display(b);
    }

    private static void display(Object x) {//하나만 만들어서 확장하는게 쉬움
        if (x instanceof A_04) {
            ((A_04)x).printGO();
        }else{
            ((B_04)x).printGO();
        }
    }
}

 

2) 다형성배열

public class ObjectPolyArray_04 {
    public static void main(String[] args) {
        //A,B 클래스를 저장할 배열 생성
        Object[] obj=new Object[2];
        obj[0]=new A_04();
        //((A_04)obj[0]).printGO();
        obj[1]=new B_04();
        display(obj);
        //((B_04)obj[1]).printGO();

        for (int i = 0; i < obj.length; i++) {
            if(obj[i] instanceof A_04){
                ((A_04)obj[i]).printGO();
            }else{
                ((B_04)obj[i]).printGO();
            }
      }
}
public class ObjectPolyArray_04 {
    public static void main(String[] args) {
        //A,B 클래스를 저장할 배열 생성
        Object[] obj=new Object[2];
        obj[0]=new A_04();
        //((A_04)obj[0]).printGO();
        obj[1]=new B_04();
        display(obj);
        //((B_04)obj[1]).printGO();
/*
        for (int i = 0; i < obj.length; i++) {
            if(obj[i] instanceof A_04){
                ((A_04)obj[i]).printGO();
            }else{
                ((B_04)obj[i]).printGO();
            }

*/        }
    private static void display(Object[] obj) {
        for (int i = 0; i < obj.length; i++) {
            if(obj[i] instanceof A_04){
                ((A_04)obj[i]).printGO();
            }else{
                ((B_04)obj[i]).printGO();
            }

        }
    }

}

3. Object 클래스의 toString() 메서드

public class Board_04 extends Object {//toString() 있음
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    //재정의

    @Override
    public String toString() {
        System.out.println(super.toString());//상위클래스의 메서드 호출(fc.java.poly.Board_04@7a79be86 출력)
        return "Board_04{" +
                "title='" + title + '\'' +
                '}';
    }
}
public class ObjectToString_04 {
    public static void main(String[] args) {
        Board_04 b=new Board_04();
        b.setTitle("게시글입니다");
        //System.out.println(b.getTitle());
        System.out.println(b.toString());//fc.java.poly.Board_04@7a79be86 -> 게시글입니다 출력하고싶음
        System.out.println(b);//fc.java.poly.Board_04@7a79be86 -> 게시글입니다 출력하고싶음 -> 재정의해야함

    }
}

이것으로 코스 1은 완주했다.. 사실 뒤에 코스 2까지 다 듣고 코스 3 듣는중인데 그냥 설명 스킵이 너무 많아서 하나하나 다 찾아봐야함 ㅠㅠ 그리고 에러도 이상한거 많이 떠서 갓피티한테 다 물어보면서 하는 중 ㅇㅅㅇ,,,