기본 콘텐츠로 건너뛰기

라벨이 이것이 자바다인 게시물 표시

Chapter13 제네릭

  제네릭<> 제네릭 타입(class<T>, interface<T>)      public class 클래스명< T > {...}      public interface 인터페이스명< T > {...} 제네릭의 장점 - 컴파일시 강한 타입 체크로 실행 시 에러를 사전에 방지. 타입변환(casting)을 제거 public class Box { private Object object; public void set (Object object) { this . object = object; } public Object get () { return object; } } public class BoxExample { public static void main (String[] args) { Box box = new Box(); box. set ( "홍길동" ); String name = (String) box. get (); System. out . println (name); box. set ( 555 ); int number = ( int ) box. get (); System. out . println (number); } } 타입 변환을 사용해야함 public class Box <T> { private T t; public void set (T t) { this . t = t; } public T get () { return t; } } public class BoxExample { public static void main (String[] args) { // TODO Auto-generated method stub Box<String> box1 = new Box<String>(); box1.

Chapter12~

  제네릭<> 제네릭 타입(class<T>, interface<T>)      public class 클래스명< T > {...}      public interface 인터페이스명< T > {...} 제네릭의 장점 - 컴파일시 강한 타입 체크로 실헹 시 에러를 사전에 방지. 타입변환(casting)을 제거 public class Box { private Object object; public void set (Object object) { this . object = object; } public Object get () { return object; } } public class BoxExample { public static void main (String[] args) { Box box = new Box(); box. set ( "홍길동" ); String name = (String) box. get (); System. out . println (name); box. set ( 555 ); int number = ( int ) box. get (); System. out . println (number); } } 타입 변환을 사용해야함 public class Box <T> { private T t; public void set (T t) { this . t = t; } public T get () { return t; } } public class BoxExample { public static void main (String[] args) { // TODO Auto-generated method stub Box<String> box1 = new Box<String>(); box1.