Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- C/C++
- php thumbnail
- 스킨 스쿠버
- flex3
- ajax
- 주식이야기
- node.js web framework
- 디즈니씨
- iBatis
- 명사 추출기
- docker
- 나의 프로젝트
- nodejs express
- ror실행
- php
- Eclipse
- 명사 뽑아내기
- scala
- 메일왕창보내는법
- 책이야기
- 도커
- rss
- ejb
- Node.js
- express for node.js
- Cross
- 나의 취미
- 베트남어
- Lift
- 명사 분석기
Archives
- Today
- Total
nkdk의 세상
자바 Language 11일째 1번 본문
package gen;
import java.util.*;
public class GenericTest4 <E extends Car>{ // Car에 대한 별명을 E로 만들었다.E타입 벡터 객체 변수
private Vector<E> user=new Vector<E>();
public void add(E element) {
user.add(element);
}
public E get(int index) {
return user.get(index);
}
public Vector<E> getAll() {
return user;
}
// 만든 걸 써먹어 보겠습니다.
}
package gen;
import java.util.*;
public class GenericTest4Main {
public static void main(String[] args) {
GenericTest4<Car> vc=new GenericTest4<Car>();
vc.add(new Car());
vc.add(new Taxi());
vc.add(new bus());
for(Car c:vc.getAll()){
c.print();
}
System.out.println("-----------------------");
GenericTest4<Taxi> vcb=new GenericTest4<Taxi>();
vcb.add(new Taxi());
vcb.add(new Taxi());
// vcb.add(new bus()); 이건 안된다
// vcb.add(new Car()); 이건 안된다
// GenericTest4<Car> cc=vcb; 이렇게 하면 안된다.
GenericTest4<? extends Car> cc=vcb; // 이와 같이 와일드 카드를 쓰면 가능하기 때문에 전체적으로 통용된다.
System.out.println("-----------------------");
printTest1(vcb);
printTest2(vc);
}
public static void printTest1(GenericTest4<Taxi> cts) {
Iterator<Taxi> iter=cts.getAll().iterator(); // iter로 cts라는 형식의 자료를 가져 올 때 사용
while(iter.hasNext()) {
Taxi taxi=iter.next();
taxi.print();
}
}
public static void printTest2(GenericTest4<? extends Car> cts) {
Iterator<? extends Car> iter=cts.getAll().iterator(); // iter로 cts라는 형식의 자료를 가져 올 때 사용
while(iter.hasNext()) {
Car c=iter.next();
c.print();
}
}
public static <T extends Car> void printTest3(GenericTest4<T> cts) {
Iterator<? extends Car> iter=cts.getAll().iterator(); // iter로 cts라는 형식의 자료를 가져 올 때 사용
while(iter.hasNext()) {
Car c=iter.next();
c.print();
}
}
}
package gen;
public class Taxi extends Car{
public void print() {
System.out.println("난 택시야");
}
public void move(){
System.out.println("승객은 3명");
}
}
package gen;
public interface Animal { // 추상 메소드만 가지고 있는 메소드 이다.
public void eat();
}
package gen;
public class Horse implements Animal{
public void eat() {
System.out.println("풀을 뜯어 먹음");
}
}
package gen;
public class Tiger implements Animal{
public void eat() {
System.out.println("짐승을 잡아 먹고 산다.");
}
}
package gen;
import java.util.*;
public class CollectionTest {
public static void main(String[] args) {
CollectionTest ct=new CollectionTest();
Tiger t1=new Tiger();
Tiger t2=new Tiger();
Horse h1=new Horse();
ArrayList<Tiger> my=new ArrayList<Tiger>(); // 타이거 타입이 아니면 담을 수 없다.
my.add(t1);
my.add(t2);
// my.add(h1); 담을 수 없게 된다. 타입이 다르기 때문이다.
// my.add(ct);
// 여기서부터 object 넣는 법
ArrayList<Object> my2=new ArrayList<Object>();
my2.add(t1);
my2.add(t2);
my2.add(ct);
String ss=new String("서소문동");
my2.add(ss);
my2.add(h1);
// 콜렉션에 대한 정리차원으로 여기서 부터 시작
boolean b=my2.isEmpty(); // 컬렉션 내의 자료 유무 여부 확인 가능
System.out.println(b);
b=my2.contains(ct);
System.out.println(b);
int f=my2.indexOf(ct);
int size=my2.size(); // <= my2 의 사이즈를 표시한다.
System.out.println("CT값은 "+(f+1)+"번째 있고 크기는 "+size);
System.out.println(my2.get(2));
my2.remove(ct); // <- ct의 값을 삭제한다.
ArrayList<Tiger> tig=new ArrayList<Tiger>();
ArrayList<Horse> hor=new ArrayList<Horse>();
ArrayList<? extends Animal> ani;
Tiger ti=new Tiger();
Horse ho=new Horse();
tig.add(ti);
hor.add(ho);
ani=tig;
for(Animal am:ani) am.eat();
ani=hor;
for(Animal am:hor) am.eat();
// 정리 차원에서 한 내용이다.
}
}
import java.util.*;
public class GenericTest4 <E extends Car>{ // Car에 대한 별명을 E로 만들었다.E타입 벡터 객체 변수
private Vector<E> user=new Vector<E>();
public void add(E element) {
user.add(element);
}
public E get(int index) {
return user.get(index);
}
public Vector<E> getAll() {
return user;
}
// 만든 걸 써먹어 보겠습니다.
}
package gen;
import java.util.*;
public class GenericTest4Main {
public static void main(String[] args) {
GenericTest4<Car> vc=new GenericTest4<Car>();
vc.add(new Car());
vc.add(new Taxi());
vc.add(new bus());
for(Car c:vc.getAll()){
c.print();
}
System.out.println("-----------------------");
GenericTest4<Taxi> vcb=new GenericTest4<Taxi>();
vcb.add(new Taxi());
vcb.add(new Taxi());
// vcb.add(new bus()); 이건 안된다
// vcb.add(new Car()); 이건 안된다
// GenericTest4<Car> cc=vcb; 이렇게 하면 안된다.
GenericTest4<? extends Car> cc=vcb; // 이와 같이 와일드 카드를 쓰면 가능하기 때문에 전체적으로 통용된다.
System.out.println("-----------------------");
printTest1(vcb);
printTest2(vc);
}
public static void printTest1(GenericTest4<Taxi> cts) {
Iterator<Taxi> iter=cts.getAll().iterator(); // iter로 cts라는 형식의 자료를 가져 올 때 사용
while(iter.hasNext()) {
Taxi taxi=iter.next();
taxi.print();
}
}
public static void printTest2(GenericTest4<? extends Car> cts) {
Iterator<? extends Car> iter=cts.getAll().iterator(); // iter로 cts라는 형식의 자료를 가져 올 때 사용
while(iter.hasNext()) {
Car c=iter.next();
c.print();
}
}
public static <T extends Car> void printTest3(GenericTest4<T> cts) {
Iterator<? extends Car> iter=cts.getAll().iterator(); // iter로 cts라는 형식의 자료를 가져 올 때 사용
while(iter.hasNext()) {
Car c=iter.next();
c.print();
}
}
}
package gen;
public class Taxi extends Car{
public void print() {
System.out.println("난 택시야");
}
public void move(){
System.out.println("승객은 3명");
}
}
package gen;
public interface Animal { // 추상 메소드만 가지고 있는 메소드 이다.
public void eat();
}
package gen;
public class Horse implements Animal{
public void eat() {
System.out.println("풀을 뜯어 먹음");
}
}
package gen;
public class Tiger implements Animal{
public void eat() {
System.out.println("짐승을 잡아 먹고 산다.");
}
}
package gen;
import java.util.*;
public class CollectionTest {
public static void main(String[] args) {
CollectionTest ct=new CollectionTest();
Tiger t1=new Tiger();
Tiger t2=new Tiger();
Horse h1=new Horse();
ArrayList<Tiger> my=new ArrayList<Tiger>(); // 타이거 타입이 아니면 담을 수 없다.
my.add(t1);
my.add(t2);
// my.add(h1); 담을 수 없게 된다. 타입이 다르기 때문이다.
// my.add(ct);
// 여기서부터 object 넣는 법
ArrayList<Object> my2=new ArrayList<Object>();
my2.add(t1);
my2.add(t2);
my2.add(ct);
String ss=new String("서소문동");
my2.add(ss);
my2.add(h1);
// 콜렉션에 대한 정리차원으로 여기서 부터 시작
boolean b=my2.isEmpty(); // 컬렉션 내의 자료 유무 여부 확인 가능
System.out.println(b);
b=my2.contains(ct);
System.out.println(b);
int f=my2.indexOf(ct);
int size=my2.size(); // <= my2 의 사이즈를 표시한다.
System.out.println("CT값은 "+(f+1)+"번째 있고 크기는 "+size);
System.out.println(my2.get(2));
my2.remove(ct); // <- ct의 값을 삭제한다.
ArrayList<Tiger> tig=new ArrayList<Tiger>();
ArrayList<Horse> hor=new ArrayList<Horse>();
ArrayList<? extends Animal> ani;
Tiger ti=new Tiger();
Horse ho=new Horse();
tig.add(ti);
hor.add(ho);
ani=tig;
for(Animal am:ani) am.eat();
ani=hor;
for(Animal am:hor) am.eat();
// 정리 차원에서 한 내용이다.
}
}