[ java ] in KIDS 글 쓴 이(By): iknowyou (Coffeeman) 날 짜 (Date): 1997년11월13일(목) 20시22분56초 ROK 제 목(Title): polymorphism in JAVA polymorphism중 하나의 의미는 함수의 parameter로서 아무거나 받을 수 있다가 있다. 자바에도 당연히 OOP이므로 polymorphism을 구현할 수 있다. Sort하는 경우를 예로 들어보자. 기존의 C로 짜는 경우 Sort하려는 data에 따라 함수를 각각 정의 해야 한다. 그러나 java에서는 꼭 그럴 필요는 없다. 코드를 예로 들자. public static void InsertionSort( Comparable[] table ){ for(int i = 1; i < table.length;i++){ int j = i; Comparable x = table[i]; while( j >= 1 && (table[j-1].compare( x ) == 1) ) { table[j] = table[j-1]; j = j - 1; } table[j] = x; } } 이 함수는 비교할 수 있는 모든 것을 어떤 것이든지 Sort할 수 있다.( 단 비교할 수 있어야 한다. 비교할 수 없다면 당연히 Sort 할 수 없다. Comparable 은 interface라고 한다. 실재로 실행을 할 때는, public static void main(String Argv[] ){ Student students[] = { new Student(2,"Lee"), new Student(5,"Kim"), new Student(3,"Park"), new Student(7,"Choi"), new Student(6,"Jin"), new Student(10,"Money"), }; for(int i = 0; i < students.length;i++) System.out.println( students[i].id + students[i].name +"\t"); System.out.println("\n"); Sort.InsertionSort( students ); System.out.println( students[i].id + students[i].name +"\t"); } } 결과는, urd [ foolish{44} ~/J/poly ] java Main 2Lee 5Kim 3Park 7Choi 6Jin 10Money 2Lee 3Park 5Kim 6Jin 7Choi 10Money 가 된다. 자세한 file은 다음에... -시간이 남으시면 complile해서 실행도 한번 해보시기를... Referances David Flanagan, Java in a Nutshell, O'Reilly & Associates, Inc., 1996. H. R. Lewis and Larry Denenberg, Data Structures & Their Algorithms, Harper CollinsPublishers, 1991. class Life extends Object implements Dynamic { Life(Life father, Life mother){} responseToThis(Life this){} responseToOthers(Life others){} } |