ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Java - Generic
    Java 2018. 1. 24. 08:44
    반응형
    Generic을 사용하면 다시 원형으로 돌아가기 위한 Casting절차를 없앨 수 있다.


    < Object - Set, Get 사용 - Casting해야 한다. >

    public class Box {
        
        private Object value;
        
        public Object getValue() {
            return value;
        }
        
        public void setValue(Object value) {
            this.value = value;
        }
        
    }


    public class Main {

        public static void main(String[] args) {
            
            Box box = new Box(); // Box안에는 무엇이 들어갈ㅈ ㅣ몰라서 Object로 선언
            box.setValue(1234);
            int value = (int) box.getValue(); // Object 형태이니까 Casting을 해줘야함
            System.out.println(value);
            
        }

    }




    < Generic - Casting 할 필요가 없다. >


    public class Box<T> { // 꼭 T로 지정할 필요는 없다. T는 type의 약자
        
        private T value; // int로 잡으면 int로 되고 String으로 잡으면 String으로 된다.

        public T getValue() {
            return value;
        }

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


    public class Main {

        public static void main(String[] args) {
            
            Box<Integer> box = new Box<Integer>();// 타입 선언때는 Class타입으로 써야 한다. 객체화하면서 들어갈 타입을 쓴다.
            // Casting할 필요가 없다.
            
            box.setValue(1246);
            int value = box.getValue();
            System.out.println(value);
            
        }

    }




    < Multi Generic <K, V> >

    public class MultiBox<K, V> { // Parameter Type이 2개
        // Key, Value
        private K key;
        private V value;
        
        public K getKey() {
            return key;
        }
        
        public void setKey(K key) {
            this.key = key;
        }
        
        public V getValue() {
            return value;
        }
        
        public void setValue(V value) {
            this.value = value;
        }
        
    }



    public class Main {

        public static void main(String[] args) {
            
            MultiBox<String, Integer> box = new MultiBox<String, Integer>();
            
    // 일일이 하나씩 넣어야 한다.
    // 보통 파라미터가 2~3개 일때 까지만 이렇게 쓴다.
            box.setKey("상무");
            box.setValue(275);
            
            String name = box.getKey();
            int score = box.getValue();
            System.out.println(name + " : " + score);
            
        }

    }



    < Generic - Class Type >

    public class Info { // Class Type으로 쓴다.
        
        private int emp_no;
        private String name;
        private int age;
        private int salary;
        private long asset;
        private float com;
        private boolean married;
        private String hobby;
        
        // getter, setter
        public int getEmp_no() {
            return emp_no;
        }
        public void setEmp_no(int emp_no) {
            this.emp_no = emp_no;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public int getSalary() {
            return salary;
        }
        public void setSalary(int salary) {
            this.salary = salary;
        }
        public long getAsset() {
            return asset;
        }
        public void setAsset(long asset) {
            this.asset = asset;
        }
        public float getCom() {
            return com;
        }
        public void setCom(float com) {
            this.com = com;
        }
        public boolean isMarried() {
            return married;
        }
        public void setMarried(boolean married) {
            this.married = married;
        }
        public String getHobby() {
            return hobby;
        }
        public void setHobby(String hobby) {
            this.hobby = hobby;
        }
        
    }



    public class Employee<T> {
        
        private T t;

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }
        
    }


    public class Main {

        public static void main(String[] args) {
            
            Info info = new Info();
            info.setEmp_no(275);
            info.setAge(26);
            
            Employee<Info> emp = new Employee<>();
            emp.setT(info);
    Info info2 = emp.getT();
    System.out.println(info2.getAge()); // info.setAge에서 26을 설정하고 Employee에 담았기 때문에 26반환
     System.out.println(info2.getHobby()); // 아직 값이 안들어가서 Null 반환
            
        }

    }

    // 담아 두었다가 꺼내 쓸 수 있다. 3개이상 되면 Generic을 쓰는 것이 낫다.
    // 여러개의 파라미터를 쓸 때는 클래스 타입으로 쓰는 것이 수월하다.



    반응형

    'Java' 카테고리의 다른 글

    Java - Collection  (0) 2018.01.25
    Java - Generic Method  (0) 2018.01.24
    Java - StringBuffer & StringBuilder  (0) 2018.01.23
    Java - String  (0) 2018.01.23
    Java - throws  (0) 2018.01.23

    댓글

Designed by Tistory.