ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA μžλ°” μ œλ„€λ¦­ (Generic)
    JAVA 2020. 8. 11. 22:25

    🎯 μžλ°” μ œλ„€λ¦­μ„ μ•Œμ•„λ³Έλ‹€

     

     

     

     

    μ œλ„€λ¦­ (Generic)

    public class Ex10_1 {
    	public static void main(String[] args) {
    		
    		Object [] arr = {"홍길동", "μ΄μˆœμ‹ ", "μœ κ΄€μˆœ"};
    		for(Object obj : arr) {
    			String name = (String)obj;
    			System.out.println("이름: "+name);
    		}
    	}
    }

    μ΅œμƒμœ„ν΄λž˜μŠ€ object둜 λ‹€μ–‘ν•œ ν•˜μœ„νƒ€μž…μ„ 담을 수 μžˆμ§€λ§Œ μƒμœ„ν΄λž˜μŠ€ μ˜€λΈŒμ νŠΈλ‘œλŠ” μžμ‹μ΄ 가진 λ©”μ†Œλ“œμ— μ ‘κ·Όν•  수 μ—†λ‹€. κ·Έλž˜μ„œ μ•Œλ§žμ€ νƒ€μž…μ„ λ°›μœΌλ €λ©΄ 맀번 ν˜•λ³€ν™˜μ΄ ν•„μš”ν•˜λ‹€. μ΄λŸ¬ν•œ λΆˆνŽΈν•œ 점을 κ°œμ„ ν•œ 방법이 μ œλ„€λ¦­μ΄λ‹€.

    public class 클래슀λͺ…<GenericType>{}
    
    클래슀λͺ…<GenericType> μ°Έμ‘°λ³€μˆ˜ = new 클래슀λͺ…<>();

    μ œλ„€λ¦­μ€ 객체생성을 ν•  λ•Œ μ‚¬μš©νƒ€μž…μ„ μ§€μ •ν•œλ‹€. ν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€μ— μ‚¬μš©ν•  수 있으며 μ»΄νŒŒμΌμ‹œ νƒ€μž…μ΄ μ •ν•΄μ§€κΈ°λ•Œλ¬Έμ— νƒ€μž…μΊμŠ€νŒ…μ΄ ν•„μš”μ—†κ³  μ•ˆμ „ν•œ μ½”λ“œμž‘μ„±μ΄ κ°€λŠ₯ν•˜λ‹€.

    ν΄λž˜μŠ€μ™€ μΈν„°νŽ˜μ΄μŠ€ 이름 뒀에 <>λ₯Ό μ‚¬μš©ν•΄ νƒ€μž… νŒŒλΌλ―Έν„°λ₯Ό μ„ μ–Έν•΄ μ‚¬μš©ν•œλ‹€.

     

    class Box{
    	Object obj;
    	public void setValue(Object obj) {
    		this.obj = obj;
    	}
    	public Object getValue() {
    		return obj;
    	}
    }
    
    public class GenericTest {
    	public static void main(String[] args) {
    		Box b = new Box();
    		b.setValue("Hello");
    		String str = (String)b.getValue();
    		System.out.println(str);
    		
    		Box b2 = new Box();
    		b2.setValue(new Date());
    		Date d = (Date)b2.getValue();
    		System.out.println(d);
    	}
    }

    Box 클래슀λ₯Ό 보면 μ΅œμƒμœ„ Object ν΄λž˜μŠ€νƒ€μž…μœΌλ‘œ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³  μžˆλ‹€. Object νƒ€μž…μ„ μ‚¬μš©ν•˜λ©΄ λ‹€μ–‘ν•œ ν•˜μœ„ν΄λž˜μŠ€ νƒ€μž…μ„ μ €μž₯ν•  수 μžˆμ§€λ§Œ μ›ν•˜λŠ” νŠΉμ • νƒ€μž…μ˜ 데이터λ₯Ό κ°€μ Έμ˜€λ €λ©΄ ν˜•λ³€ν™˜μ΄ ν•„μš”ν•˜λ‹€. 

    import java.util.ArrayList;
    import java.util.Date;
    
    //Object λŒ€μ‹ μ— t(type) μ˜λ―Έλ‘œμ„œ μ°Έμ‘°ν˜• νƒ€μž…λ§Œ 지정 κ°€λŠ₯
    // tλŠ” λ°•μŠ€ 객체λ₯Ό 생성할 λ–Ό ꡬ체적인 νƒ€μž…μœΌλ‘œ λ³€κ²½κ°€λŠ₯
    
    class Box<T>{
    	T obj;
    	public void setValue(T obj) {
    		this.obj = obj;
    	}
    	public T getValue() {
    		return obj;
    	}
    }
    
    public class GenericTest2 {
    	public static void main(String[] args) {
    		//μ œλ„€λ¦­μŠ€ νƒ€μž…(generics type)
    		Box<String> b = new Box<String>();
    		b.setValue("Hello");
    		String x = b.getValue();
    		System.out.println(x);
    		Box<Date> b2 = new Box<>();
    		b2.setValue(new Date());
    		Date d = b2.getValue();
    		System.out.println(d);
    		
    		Box<Integer> b3 = new Box<>();
    		b3.setValue(100);
    		int xxx = b3.getValue();
    		System.out.println(xxx);
    	}
    }

    μ œλ„€λ¦­ 방법을 μ‚¬μš©ν•˜λ©΄ 객체생성할 λ•Œ 데이터듀을 슀트링 νƒ€μž…μœΌλ‘œ κ²°μ •ν•  수 μžˆλ‹€.

     

     

    μ‚¬μš©κ°€λŠ₯ν•œ νƒ€μž… μ œν•œ

    class Pet{}
    class Cat extends Pet{}
    class Dog extends Pet{}
    
    class Box2<T extends Pet>{
    	T obj;
    	public void setValue(T obj) {
    		this.obj=obj;
    	}public T getValue() {
    		return obj;
    	}
    }
    public class Ex10_3 {
    	public static void main(String[] args) {
    		Box2<Pet> b = new Box2<Pet>();
    		Box2<Cat> b2 = new Box2<Cat>();
    		Box2<Dog> b3 = new Box2<Dog>();
    		//Box2<Object> b4 = new Box2<>();
    	}
    }

    νŠΉμ • 클래슀λ₯Ό μƒμ†ν•˜λŠ” μ• λ“€λ§Œ νŠΉμ • νƒ€μž…μœΌλ‘œ λ°›κ² λ‹€κ³  νŒŒλΌλ―Έν„° νƒ€μž…μ„ μ œν•œν•  수 μžˆλ‹€. μœ„μ™€κ°™μ΄ extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.

     

     

    μ œλ„ˆλ¦­ λ©”μ†Œλ“œ

    class Vehicle{}
    class Truck extends Vehicle{}
    class Car extends Vehicle{}
    
    public class Ex10_4 {
    	public static <p> void method(P p) {}
    	public static <P p> method2(P p){
    		return p;
    	}
    	
    	public static void main(String[] args) {
    		method(new Vehicle());
    		method(new Truck());
    		method(new Car());
    		
    		Vehicle v = method2(new Vehicle());
    		Truck t = method2(new Truck());
    		Car c = method2(new Car());
    	}
    }

    λ©”μ†Œλ“œμ˜ λ¦¬ν„΄νƒ€μž… μ•žμ— νƒ€μž… νŒŒλΌλ―Έν„°λ₯Ό μ§€μ •ν•˜κ³  λ©”μ†Œλ“œμ˜ λ¦¬ν„΄νƒ€μž…μ΄λ‚˜ νŒŒλΌλ―Έν„°λ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.

     

     

    😜 NEXT : μžλ°” μ»¬λ ‰μ…˜ κ³΅λΆ€ν•˜κΈ°

    λ°˜μ‘ν˜•

    λŒ“κΈ€

κ°œλ°œκ³΅λΆ€