Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 안드로이드
- sub query
- 시큐어코딩
- 상속
- webpack
- kotlin
- git
- 답글
- java
- Android
- 버전일치
- 스프링
- 쿠키
- FIle
- java#왕초보
- 스타일보험
- SQL
- 게시판
- 코틀린
- React
- 왕초보
- 오버라이드
- degit
- Spring
- 함수
- snowpack
- Spinner
- 숫자
- parcel
- 미니게임
Archives
- Today
- Total
YSHUSH
Generic 본문
˙ Generic == template(형태)
- 자료형 변수
- 같은 클래스에서 다양한 자료형을 사용하고 싶은 경우, 설정하는 요소
Box<Integer> box = new Box<Integer>( 123 );
box.setTemp( 234 );
System.out.println(box.getTemp());
Box<String> sbox = new Box<String>( "hello" );
sbox.setTemp("world");
System.out.println(sbox.getTemp());
// Box<int> tbox = new Box<int>(1); 일반 자료형은 사용 못함
BoxMap<Integer, String> bmap = new BoxMap<Integer, String>(1001, "홍길동");
System.out.println(bmap.getKey());
System.out.println(bmap.getValue());
BoxMap<String, String> smap = new BoxMap<String, String>("apple", "사과");
/*
BoxMap<Double, Integer> _smap
BoxMap<Integer, Integer>
BoxMap<Double, Double>
*/ // Java에서는 막아놨음
}
}
class Box<T>{ // T = Integer, String
T temp;
public Box(T temp) {
this.temp = temp;
}
public T getTemp() {
return temp;
}
public void setTemp(T temp) {
this.temp = temp;
}
}
class BoxMap<KEY, VALUE>{
KEY key;
VALUE value;
public BoxMap(KEY key, VALUE value) {
this.key = key;
this.value = value;
}
public KEY getKey() {
return key;
}
public void setKey(KEY key) {
this.key = key;
}
public VALUE getValue() {
return value;
}
public void setValue(VALUE value) {
this.value = value;
}
}
'Coding > Java' 카테고리의 다른 글
Array list 2 (0) | 2021.12.20 |
---|---|
Array list (0) | 2021.12.20 |
Static (0) | 2021.12.17 |
Final (0) | 2021.12.17 |
Name card(interface) (0) | 2021.12.17 |