자바공부하기 107

upcasting과 downcasting의 실제 구현

 

import java.util.Vector;

 

public class upDownCasting {

public static void main(String[] args) {

String name = new String("홍길동");

Integer id = new Integer(1000);

Vector v = new Vector();

v.addElement(name);

v.addElement(id);

 

//upcasting

Object obj1 = v.elementAt(0);

Object obj1 = v.elementAt(1);

 

//Downcasting

String str = (String)obj1;

Integer num = (Integer)obj2;

System.out.println("이름:" + name + "번호:" +num);

String str2 = (String)v.elementAt(0);

Integer num2 = (Integer)v.elementAt(1);

System.out.println("이름" + name + "번호:" + num);

}

}

 

- Vector는 객체 저장소 / Object형만을 저장할 수 있음

- 저장은 Upcasting / 추출은 Downcasting

 

 

 

 

+ Recent posts