정보보안공부

Java2_day04 본문

Language/Java2

Java2_day04

Steady_sp 2017. 3. 16. 02:00

*** generic_ex - Apple

 

 

-> Apple클래스 생성

 

*** generic_ex - Box

 

 

-> public class Box<T> {

    private T t; //원래 int나 Object자리였는데 T로 설정

    public T get()    { return t; }

    public set(T t)     { this.t = t; }

 

*** generic_ex - BoxExample

 

 

-> Box<String> b1 = new Box<String>();

-> b1.set("hello");

-> String str = b1.get(); get()은 hello이므로 str에 hello값 대입

 

-> Box<Integer> b2 = new Box<Integer>();

-> b2.set(6);

-> int value = b2.get(); get()은 6이므로 value에 6 대입

 

-> Box<Apple> b3 = new Box<Apple>();

-> b3.set(new Apple());

-> Apple a = b3.get(); get()은 new Apple()의 주소값이므로 a 에 대입

 

***  none_generic - Apple

 

 

-> Apple클래스 생성

 

*** none_generic - OBox

 

 

-> public class OBox { private Object object;

    public void set(Object object) { this.object = object; }

    public Object get() { return object; } } 선언

 

*** none_generic - OBoxExample

 

 

-> OBox box = new OBox();

-> box.set(new Apple());

    Apple apple = (Apple) box.get();    //object에서 return값 Apple로 캐스팅

-> box.set("홍길동")

    String str = (String) box.get();    //object에서 return값 String으로 캐스팅

-> box.set(100)

    int num = (int) box.get();        //object에서 return 값 int로 캐스팅

 

-> generic과 object차이 object로 할경우 캐스팅해야한다.

 

*** generic_method - Pair

 

 

-> Pair<K,V> { private K key; private V value;

    public Pair(K key, V value) { this.key = key; this.value = value; }

    public void setKey(K key) { this.key = key; }

    public void setValue(V value) { this.value = value; }

    public K getKey() { return key;} public V getValue() { reutn value; } } 선언

 

*** generic_method - Util

 

 

-> public static <K,V> boolean compare(Pair<K,V> p1, Pair<K,V> p2) {

    boolean keyCompare = p1.getKey().equals(p2.getKey());

    boolean valueCompare = p1.getValue().equals(p2.getValue());

    return boolean keyCompare && valueCompare; }

    p1의 key값 p2의 key값 과 p1의 value값 p2의 value값이 같은지 비교해서

    key와 value 참이면 compare메소드는 참을 리턴한다.

-> public static <K,V> void repair(Pair<K,V> p1, Pair<K,V> p2) {

    p1.setKey(p2.getKey());        p1.setValue(p2.getValue());

    p2의 key값을 p1의 key값으로 p2의 value값을 p1의 value값으로 이동한다.

 

*** generic_method - CompareEx

 

 

-> Pair<Integer,String> p1 = new Pair<>(1, "사과");

    Pair<Integer,String> p2 = new Pair<Integer,String>(1, "사과);

    new Pair<타입> 타입 생략가능하다.

    boolean result1 = Util.compare(p1,p2) compare메소드실행후 if문 실행

    논리적으로 동등한 객체입니다. 출력

    p1.setKey(2); p1.setValue("바나나"); 선언하면 compare메소드실행후

    논리적으로 동등하지 않는 객체입니다. 출력

 

-> Pair<String,String> p3 = new Pair<>("user1", "홍길동");

    Pair<String,String> p4 = new Pair<>("user2", "홍길순");

    boolean result2 = Util.compare(p3,p4) compare메소드실행후 if문 실행

    논리적으로 동등하지 않는 객체입니다. 출력

 

-> Util.repair(p3,p4) 실행하면 p4객체의 맴버 값을 p3으로 전달한다.

    boolean result3 = Util.compare(p3,p4) compare메소드 실행하여 repair값 결과

    확인한다.

 

*** mutl_type - Car

 

 

-> Car클래스에 public String toString() { return "car" ; }를 선언하여

    Car클래스가 실행될때 리턴값 car불러온다.

 

*** mutl_type - Tv

 

 

-> Tv클래스에 public String toString() { return "tv" ; }를 선언하여

    Tv클래스가 실행될때 리턴값 tv 불러온다.

 

*** mutl_type - Model

 

 

-> Model 클래스 작성

 

*** mutl_type - Product

 

 

-> public class Product<K,M> 작성{ private T kind; private M model;

    public void setKind(T kind) { this.kind = kind; }

    public void setModel(M model) { this.model = model; }

    public T getKind() { return kind; }

    public M getModel() { return model; } } 작성

 

*** mutl_type - ProductExample

 

 

-> Product<Tv, String> p1 = new Product<>();

    p1.setKind(new Tv());  p1.setModel("스마트Tv");

    Tv tv = p1.getKind();  String tvModel = p1.getModel();

    set과 get으로 set으로 입력 get으로 값불러온다.

 

-> Product<Car, String> p2 = new Product<>();

    p2.setKind(new Car());  p2.setModel("디젤");

    Car car = p2.getKind(); String carModel = p2.getModel();

 

-> Product<String, Double> p3 = new Product<>();

    p3.setKind("sonata");  p3.setModel(12.345);

    String kind = p3.getKind();  double model = p3.getModel();

 

'Language > Java2' 카테고리의 다른 글

Java2_day06  (0) 2017.03.17
Java2_day05  (0) 2017.03.16
Java2_day03  (0) 2017.03.14
Java2_day02  (0) 2017.03.11
Java2_day01  (0) 2017.03.11
Comments