사용자 도구

사이트 도구


study:java:javachobo:appendix

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
study:java:javachobo:appendix [2010/03/19 22:22]
gauryan
study:java:javachobo:appendix [2010/03/21 14:48]
gauryan
줄 165: 줄 165:
 } }
 </code> </code>
-위와 같이 선언된 메서드가 있을 때, 이 메서드의 매개변수로는 Array<Unit>타입의 변수만 사용할 수 있다. 즉, ArrayList<Unit> list = new ArrayList<Unit>();으로 생성된 객체만 매개변수로 사용될 수 있다는 뜻이다.+위와 같이 선언된 메서드가 있을 때, 이 메서드의 매개변수로는 ArrayList<Unit>타입의 변수만 사용할 수 있다. 즉, ArrayList<Unit> list = new ArrayList<Unit>();으로 생성된 객체만 매개변수로 사용될 수 있다는 뜻이다.
  
 <code java> <code java>
줄 193: 줄 193:
 **[주의]** 여기서 만일 Unit이 클래스가 아닌 인터페이스라 할지라도 키워드로 'implements'를 사용하지 않고 클래스와 동일하게 'extends'를 사용한다는 것에 주의하자. **[주의]** 여기서 만일 Unit이 클래스가 아닌 인터페이스라 할지라도 키워드로 'implements'를 사용하지 않고 클래스와 동일하게 'extends'를 사용한다는 것에 주의하자.
  
-예제 : /GenericsEx1.java <code java>+  * 예제 : /GenericsEx1.java <code java>
 import java.util.*; import java.util.*;
  
줄 229: 줄 229:
             System.out.println(u);             System.out.println(u);
         }         }
-    } 
-} 
-</code> 
- 
-Collections 클래스의 sort() 메서드의 선언부를 보면 다음과 같다. 
-<code java> 
-public static <T extends Comparable<? super T>> void sort(List<T> list) 
-              ---------------------------------           ------- 
-                           (2)                              (1) 
-</code> 
-  - ArrayList 와 같이 List 인터페이스를 구현한 컬렉션을 매개변수의 타입으로 정의하고 있다. 그리고 그 컬렉션에는 'T'라는 타입의 객체를 저장하도록 선언되어 있다. 
-  - 'T'는 Comparable 인터페이스를 구현한 클래스의 타입이어야 하며 'T' 또는 그 조상의 타입을 비교하는 Comparable 이어야 한다는 것을 의미한다. 
-<code java> 
-public interface Comparable<T> { 
-    public int compareTo(T o); // 지정한 타입 T를 매개변수로 한다. 
-} 
-</code> 
-Comparable 의 실제소스는 위와 같으며 이를 구현한 클래스의 예는 다음과 같다. 
-<code java> 
-class Student extends Person implements Comparable<Person> { 
-    Student(int id, String name) { 
-        super(id, name); 
-    } 
-     
-    public int compareTo(Person o) { 
-        return this.id - o.id; 
-    } 
-} 
- 
-class Person { 
-    int id; 
-    String name; 
-     
-    Person(int id, String name) { 
-        this.id = id; 
-        this.name = name; 
-    } 
-     
-    public String toString() { 
-        return id + ":" + name; 
-    } 
-} 
-</code> 
-Collections 의 sort()의 매개변수가 Comparable에 사용될 수 있는 타입이 Comparable 을 구현하는 클래스(Student)나 그 조상(Person)일 수 있다고 하였기 때문에 Student 객체를 담은 ArrayList 는 sort() 의 매개변수가 될 수 있는 모든 조건을 충족시킨다. 
- 
-예제 : GenericsEx2.java <code java> 
-import java.util.*; 
- 
-class Unit {} 
-class Tank extends Unit {} 
-class Dropship extends Unit {} 
- 
-class GenericsEx2 { 
-    ArrayList<Student> list = new ArrayList<Student>(); 
-    list.add(new Student(10, "Kim")); 
-    list.add(new Student(40, "Lee")); 
-    list.add(new Student(20, "Park")); 
-    list.add(new Student(30, "Jung")); 
-    Conllections.sort(list); 
-     
-    for(Person p : list) { 
-        System.out.println(p); 
-    } 
-} 
- 
-class Student extends Person implements Comparable<Person> { 
-    Student(int id, String name) { 
-        super(id, name); 
-    } 
-     
-    // Comparable<Person> 이므로 Person 타입의 매개변수를 선언. 
-    public int compareTo(Person o) { 
-        return this.id - o.id; 
-    } 
-} 
- 
-class Person { 
-    int id; 
-    String name; 
-     
-    Person(int id, String name) { 
-        this.id = id; 
-        this.name = name; 
-    } 
-     
-    public String toString() { 
-        return id + ":" + name; 
     }     }
 } }
줄 353: 줄 266:
 </code> </code>
  
-예제 : EnumEx.java <code java>+  * 예제 : EnumEx.java <code java>
 import java.util.*; import java.util.*;
  
줄 426: 줄 339:
 가변인수를 사용할 때 한 가지 주의해야할 점은 오버로딩을 하는 경우인데, 먼저 예제를 살펴보고 그 다음에 자세히 설명하겠다. 가변인수를 사용할 때 한 가지 주의해야할 점은 오버로딩을 하는 경우인데, 먼저 예제를 살펴보고 그 다음에 자세히 설명하겠다.
  
-예제 : VarArgsEx1.java <code java>+  * 예제 : VarArgsEx1.java <code java>
 class VarArgsEx1 { class VarArgsEx1 {
     public static void main(String[] args) {     public static void main(String[] args) {
줄 475: 줄 388:
 </code> </code>
  
-예제 : StaticImportEx1.java <code java>+  * 예제 : StaticImportEx1.java <code java>
 import static java.lang.System.out; import static java.lang.System.out;
 import static java.lang.Math.*; import static java.lang.Math.*;
study/java/javachobo/appendix.txt · 마지막으로 수정됨: 2010/03/21 14:48 저자 gauryan