This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ObjectBox objectBox1 = new ObjectBox(); | |
objectBox1.store(new Melon()); // 컴파일통과 | |
ObjectBox objectBox2 = new ObjectBox(): | |
objectBox2.store(new Apple()); // 컴파일통과 | |
Apple apple = (Apple)objectBox2.getItem(); // 컴파일통과 | |
Melon melon = (Melon)objectBox2.getItem(); // 컴파일통과 런타임오류 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AppleBox appleBox = new AppleBox(); | |
appleBox.store(new Melon()); // 컴파일오류발생 | |
MelonBox melonBox = new MelonBox(): | |
melonBox.store(new Apple()); // 컴파일오류발생 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ObjectBox { | |
Object item; | |
public void store(Object item) { | |
this.item = item; | |
} | |
public Object getItem() { | |
return item; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppleBox { | |
Apple item; | |
public void store(Apple item) { | |
this.item = item; | |
} | |
public Apple getItem() { | |
return item; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> strs = Arrays.asList("a", "b", "c", "d"); | |
List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6); | |
int size1 = GenericMethod.getListSize(strs); | |
int size2 = GenericMethod.getListSize(ints); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Comparable 인터페이스를 구현해 봤다면 알 것이다. | |
// public interface Comparable<T> { | |
// public int compareTo(T o); | |
// } | |
class GenericInterfaceImpl implements Generic<String, Integer> { | |
@Override | |
public String doSomething(Integer t) { | |
return t.toString(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Generics<String> genercis = new Generics<>(); | |
generics.set("Hello"); | |
// list 사용할 때와 비교해보자. 똑같지 않나? | |
List<String> strs = new ArrayList<>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericMethod { | |
public static <T> int getListSize(List<T> list) { | |
int count = 0; | |
for (T t : list) { | |
count++; | |
} | |
return count; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Generic<T1, T2> { | |
T1 doSomething(T2 t); | |
T2 doSomething2(T1 t); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Generics<T> { | |
private T t; | |
public void set(T t) { | |
this.t = t; | |
} | |
public T get() { | |
return t; | |
} |