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
- 마지막에 배열의 foreach구문이 틀린것같은데 ...... 저게왜틀린건지나는잘모르겠슴다.
- while문이 틀린이유?? math.random()을 사용해서푸는법?
- ㅓㅂ
- 출처:구멍가게코딩단-코배스(개정판)
- form.getImageFies 오타났음
- 그럼 int배열의 deefault값은?????
- 행열. 2중반복문..
- (참고로 boolean 배열의 default 값은 false 이다.
- boolean배열
- 생활코딩
- bindingresult 쓰니까 에러났다. 어떻게해야하냐;;
Archives
- Today
- Total
푸린세스
지네릭 타입의 형변환2 본문
package ex12;
import java.util.ArrayList;
class Fruit implements Eatable{
public String toString() { return "Fruit";}
}
class Apple extends Fruit { public String toString() {return "Apple";}}
class Grape extends Fruit { public String toString() {return "Grape";}}
class Toy { public String toString() {return "Toy";}}
interface Eatable{}
class Ex12_3 {
public static void main(String[] args) {
FruitBox<Fruit> fbox = new FruitBox<Fruit>();
FruitBox<Apple> abox = new FruitBox<Apple>();
//일치하는게 정석.
FruitBox<? extends Fruit> f2box = new FruitBox<Fruit>();
FruitBox<? extends Fruit> a2box = new FruitBox<Apple>();
//형변환이 생략되어있다는 뜻
FruitBox<? extends Fruit> f22box =(FruitBox<? extends Fruit>) new FruitBox<Fruit>();
//좌변과 우변이 타입불일치 , 대입된 지네릭타입이 다르다.
//형변환 필요.
//FruitBox<Apple> -> FruitBox<? extends Fruit> 가능
//FruitBox<? extends Fruit> -> FruitBox<Apple> 가능? 가능하다. but생략불가라 꼭 써줘야함..****
//FruitBox<Apple> a3box = (FruitBox<Apple>) new FruitBox<? extends Fruit>();
//위의 경우 new연산자 뒤에 T오면 안됨. 와일드카드도올수없음?
FruitBox<Apple> a33box = (FruitBox<Apple>)a2box; //OK.경고발생
//a2box 타입이 명확하지 않는 와일드 카드 -> a33box 명확한타입으로 바꾸려고 하니까..
//형변환은 항상 () 형변환써주면된다.
//생략했는데 에러안나면 생략가능한거고, 생략했는데 에러나면 필요한것임..
}
}
class FruitBox<T extends Fruit & Eatable> extends Box<T>{}
class Box<T>{
ArrayList<T> list = new ArrayList<T>();
void add(T item) {list.add(item);}
T get(int i) {return list.get(i);}
int size() {return list.size();}
public String toString() {return list.toString();}
}