Day 15(8/19) Generic

2016. 8. 23. 15:57Programming/Java

**오늘 배운 내용**

1) 객체 생성시 데이터타입 맞추기

2) Generic 활용



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Animal{
     Object name;
     int int_name;
     public Animal(Object name){
          this.name = name;
     }
     public Animal(int int_name){
          this.int_name = int_name;
     }
}
 
//해결법
 //1. 필드추가 + 생성자 오버로드 => 필드와 생성자가 계속 추가되면 코드가 길어지고 유지보수가 힘듬.
 //2. name dataType 변경(모든 클래스의 조상인 Object를 데이터타입으로) -> 코드가 짧아지고 flexible해짐
 //3. 제네릭 -> 2번 방식 고수하다 심각한 오류 때문에 추가됨

 //제네릭: 전역변수(필드)의 데이터 타입을 유동적으로 해줌.
public class L01Generic {
     public static void main(String[] args) {
          Animal ani1 = new Animal("코끼리");
          Animal ani2 = new Animal(0011); //시조새
     }
}
cs



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
32
33
34
35
36
import javax.sound.midi.MidiDevice.Info;
 
class Person{ //사람 클래스를 저장
     Object info;
     public Person(Object info){
          this.info = info;
     }//생성자 end
}//class end
 
class Employee{
     int rank;
     public Employee(int rank){
         this.rank=rank;
     }
}
 
class Student{
     int grade;
     public Student(int grade){
         this.grade = grade;
     }
}
 
public class L02GenericCause {
     public static void main(String[] args) {
          Person stu = new Person(new Student(80)); //Student 저장
          Person emp = new Person(100); //Employee 저장(실수한 부분)
      Student stu_info = (Student)stu.info; //Object은 모든 데이터 타입의 조상이므로 모든 클래스로 형변환 가능
          System.out.println(stu_info.grade); //힌트: Object 수업
          System.out.println(((Employee)emp.info).rank);
  
  //이유: emp.info의 데이터 타입은 Integer, 그러므로 Employee로 형변환 불가
  //컴파일시 발견되지 않는 심각한 오류
 
    }
}
cs



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
32
33
34
35
36
37
38
39
40
class Pers<extends Personable, U, V>{
 //extends, implements 상관 없이 부모 또는 조상 클래스로 Personable을 가지고 있는 class만 사용 가능
 
 T info; //T는 <> 안에 지정한 데이터 타입이 됨(아무것도 없으면 Object 타입)
          //제네릭으로 사용할 수 있는 데이터 타입은 class 뿐이다.
 U user;
 V v;
 public Pers(T info){ 
     this.info = info;
     }
}
 
interface Personable{}
 
class Stu implements Personable{
     int grade;
     public Stu(int grade){
         this.grade = grade;
     }
}
 
class Emp implements Personable{
     int rank;
     public Emp(int rank){
          this.rank = rank;
     }
}
 
class Ani{}
 
public class L03GenericEx {
     public static void main(String[] args) {
         Pers<Stu> stu = new Pers<Stu>(new Stu(90));
         //Pers<Integer> int_info = new Pers<Integer>(39);
              System.out.println(stu.info.grade);
  
         //Pers 클래스의 필드 info의 데이터 타입이 사람 객체만 받고 싶다.
         Pers p = new Pers(new Ani());
     }
}
cs


'Programming > Java' 카테고리의 다른 글

Day 17(8/24) Try~Catch, throws  (0) 2016.08.26
Day 16(8/22) ArrayList, HashMap, HashSet  (0) 2016.08.23
Day 14(8/18) Final, Enum  (0) 2016.08.23
Day 13(8/17) public, private  (0) 2016.08.23
Day 12(8/16) Equals  (0) 2016.08.23