[09]object클래스

Object 클래스

  • Object 클래스
  • 자바 클래스의 최고 조상 : Object
  • 자바의 모든 클래스는 Object 클래스의 후손
  • 자바의 모든 클래스는 Object 클래스에 대입 가능
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
class Print{
	/*
	 * static : 클래스명으로 직접접근 가능.
	 * 클래스명.멤버변수
	 * 클래스명.멤버함수()
	 * method overloading : 매개변수의 자료형과 갯수로 구분.(함수이름이같아도됨)
	 */
/*	public static void view(Integer a){
		System.out.println(a.toString());
	}
	public static void view(Double a){
		System.out.println(a.toString());
	}
	public static void view(String a){
		System.out.println(a.toString());
	}
	//이렇게 해도 틀린건 아님
	*/
	
	public static void view(Object obj){
		System.out.println(obj.toString());
	}
	
	public static Integer disp1(){
		return new Integer(3);
	}
	
	public static Double disp2(){
		return 5.6; //이렇게 해도 autoboxing 됨.
	}
	
	public static Object disp3(){
		return new Integer(9); //틀린문법 아님.
	}
	
	public static Object disp4(){
		return new Double(9);
	}
		
	public static Object disp5(){
		return "Happy";
	}
	
}//print class
  • 가장 일반적인 형태
1
2
3
4
5
6
7
8
Integer inte=new Integer(3); 
System.out.println(inte);				//	3
System.out.println(inte.toString()); 	// "3" 같은 의미.


String str=new String("HAPPY");
System.out.println(str);			//"HAPPY"
System.out.println(str.toString());	//"HAPPY"
  • 다형성
1
2
3
4
5
6
7
8
9
10
11
12
13
Object obj=new Integer(5);	// 5
System.out.println(obj);

obj=new String("Soldesk");
System.out.println(obj); 	// Soldesk

obj=new Double(3.4);
System.out.println(obj); 	// 3.4


Double dou=(Double) obj;	// Double dou=obj; 하면 에러
                            // Object는 조상이기 때문에 형변환 후 대입. > Double dou=(Double) obj;
System.out.println(dou);	// 3.4

메소드와 관련된 Object 클래스

1. 매개변수가 Object 클래스

  • Object 클래스에 들어갈 수 있다. 후손들이기 때문
  • Object 하나로 받아도 됨.
1
2
3
4
5
6
7
8
9
Print.view(new Integer(3));
Print.view(new Double(5.6));
Print.view(new String("Happy"));
Print.view(new Father());

// autoboxing
// > 기본형값이 전달되면 참조형으로 자동으로 변환된다.
Print.view(7);
Print.view(8.9);

2. 메소드의 리턴형이 Object 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Integer a=Print.disp1();
System.out.println(a); //3

Double b=Print.disp2();
System.out.println(b); //5.6


Object obj2=Print.disp3();
System.out.println(obj.toString()); //3.4

Double c=(Double) Print.disp4(); //부모가 자식한테 들어갈땐 형변환.
System.out.println(c);			//9.0

String d=(String) Print.disp5(); //부모가 자식한테 들어갈땐 형변환.
System.out.println(d);			//Happy

Object 클래스 : 자바의 최상위 클래스

예시 클래스들

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
class Screen{
	public String getData(){
		return "";
	}
}//screen


class TypeA extends Screen{
	@Override //재정의
	public String getData() {
		return "기생충";
	}
}//typea


class TypeB extends Screen{
	@Override
	public String getData() {
		return "어벤져스";
	}
}//typeb



class Picture{
	/*
	 * 1) Method Overloading
	public static void dispStar(TypeA ta){
		System.out.println(ta.getData());
	}
	public static void dispStar(TypeB tb){
		System.out.println(tb.getData());
	}
	*/
	
	/* 2) 다형성
	
	public static void dispStar(Screen scr){
		System.out.println(scr.getData());
	}
	
	*/
	
	// 3) 다형성
	public static void dispStar(Object obj){
		Screen scr=(Screen) obj; //형변환
		System.out.println(scr.getData());
		
	}
}//picture
  • 다형성
1
2
3
4
5
6
7
8
9
10
11
12
Screen scr=new TypeA();
System.out.println(scr.getData()); //기생충

scr=new TypeB();
System.out.println(scr.getData()); //어벤져스


TypeA ta=new TypeA();
TypeB tb=new TypeB();

Picture.dispStar(ta);	//기생충
Picture.dispStar(tb);	//어벤져스