java:anon_inner_classes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:anon_inner_classes [2020/03/23 09:15] – [Πρόσβαση στις τοπικές μεταβλητές και στις μεταβλητές της εξωτερικής κλάσης] gthanos | java:anon_inner_classes [2023/03/23 20:43] (current) – [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface] gthanos | ||
---|---|---|---|
Line 12: | Line 12: | ||
===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface ===== | ===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface ===== | ||
- | Στο παρακάτω παράδειγμα ορίζουμε την κλάση **Student** η οποία έχει τα τρία (3) πεδία **firstName**, | + | Στο παρακάτω παράδειγμα ορίζουμε την κλάση **Student** η οποία έχει τα τρία (3) πεδία **firstName**, |
- | + | ||
- | <code txt students.txt> | + | |
- | 4596 Super Man | + | |
- | 5819 Peter Pan | + | |
- | 1694 Susan Brown | + | |
- | 7895 Lena Smith | + | |
- | 3256 Oscar Gonzales | + | |
- | 1243 Edgar Wallace | + | |
- | 3265 Kate Gordon | + | |
- | 4377 Leda Evans | + | |
- | 1118 David Lerroy | + | |
- | 8744 Amelia Dray | + | |
- | </ | + | |
<code java Student.java> | <code java Student.java> | ||
Line 48: | Line 35: | ||
| | ||
// First anonymous comparator class | // First anonymous comparator class | ||
- | Comparator< | + | |
public int compare(Student st1, Student st2) { | public int compare(Student st1, Student st2) { | ||
int cmp = st1.lastName.compareTo(st2.lastName); | int cmp = st1.lastName.compareTo(st2.lastName); | ||
Line 59: | Line 46: | ||
}; | }; | ||
+ | public static void sortLexicographically(List< | ||
+ | Collections.sort(students, | ||
+ | } | ||
+ | | ||
+ | public static void sortByAEM(List< | ||
+ | // Second anonymous comparator class | ||
+ | Collections.sort(students, | ||
+ | public int compare(Student st1, Student st2) { | ||
+ | int cmp = st1.AEM - st2.AEM; | ||
+ | if(cmp==0) | ||
+ | cmp = st1.lastName.compareTo(st2.lastName); | ||
+ | if(cmp==0) | ||
+ | cmp = st1.firstName.compareTo(st2.firstName); | ||
+ | return cmp; | ||
+ | } | ||
+ | } | ||
+ | ); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Στη μέθοδο **// | ||
+ | * με τη σειρά που τα διαβάσαμε | ||
+ | * με λεξικογραφική σειρά. | ||
+ | * με βάση το ΑΕΜ | ||
+ | |||
+ | <code txt students.txt> | ||
+ | 4596 Super Man | ||
+ | 5819 Peter Pan | ||
+ | 1694 Susan Brown | ||
+ | 7895 Lena Smith | ||
+ | 3256 Oscar Gonzales | ||
+ | 1243 Edgar Wallace | ||
+ | 3265 Kate Gordon | ||
+ | 4377 Leda Evans | ||
+ | 1118 David Lerroy | ||
+ | 8744 Amelia Dray | ||
+ | </ | ||
+ | |||
+ | <code java SortListOfStudents.java> | ||
+ | import java.io.*; | ||
+ | import java.util.*; | ||
+ | |||
+ | public class SortListOfStudents { | ||
+ | | ||
public static void readFromFile(List< | public static void readFromFile(List< | ||
try(Scanner sc = new Scanner(f)) { | try(Scanner sc = new Scanner(f)) { | ||
Line 73: | Line 105: | ||
System.exit(-1); | System.exit(-1); | ||
} | } | ||
- | } | ||
- | | ||
- | public void sortLexicographically(List< | ||
- | Collections.sort(students, | ||
- | } | ||
- | | ||
- | public void sortByAEM(List< | ||
- | // Second anonymous comparator class | ||
- | Collections.sort(students, | ||
- | public int compare(Student st1, Student st2) { | ||
- | int cmp = cmp = st1.AEM - st2.AEM; | ||
- | if(cmp==0) | ||
- | cmp = st1.lastName.compareTo(st2.lastName); | ||
- | if(cmp==0) | ||
- | cmp = st1.firstName.compareTo(st2.firstName); | ||
- | return cmp; | ||
- | } | ||
- | } | ||
- | ); | ||
} | } | ||
| | ||
public static String toString(List< | public static String toString(List< | ||
- | | + | |
for(Student st : students) | for(Student st : students) | ||
str.append(st+" | str.append(st+" | ||
Line 111: | Line 124: | ||
System.out.println(" | System.out.println(" | ||
System.out.println(toString(students)); | System.out.println(toString(students)); | ||
- | | + | |
System.out.println(" | System.out.println(" | ||
System.out.println(toString(students)); | System.out.println(toString(students)); | ||
- | | + | |
System.out.println(" | System.out.println(" | ||
System.out.println(toString(students)); | System.out.println(toString(students)); | ||
} | } | ||
- | | ||
} | } | ||
</ | </ | ||
+ | |||
===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως επέκταση υφιστάμενης κλάσης ===== | ===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως επέκταση υφιστάμενης κλάσης ===== | ||
Line 146: | Line 159: | ||
} | } | ||
| | ||
- | abstract class BasicComparator implements Comparator< | + | |
- | + | ||
} | } | ||
| | ||
- | Comparator< | + | |
public int compare(Student st1, Student st2) { | public int compare(Student st1, Student st2) { | ||
int cmp = st1.lastName.compareTo(st2.lastName); | int cmp = st1.lastName.compareTo(st2.lastName); | ||
Line 160: | Line 172: | ||
} | } | ||
}; | }; | ||
- | |||
- | public static void readFromFile(List< | ||
- | try(Scanner sc = new Scanner(f)) { | ||
- | while(sc.hasNextLine()) { | ||
- | students.add( new Student(sc.nextInt(), | ||
- | } | ||
- | } | ||
- | catch(FileNotFoundException ex) { | ||
- | System.out.println(" | ||
- | } | ||
- | catch(InputMismatchException ex) { | ||
- | System.out.println(" | ||
- | ex.printStackTrace(); | ||
- | System.exit(-1); | ||
- | } | ||
- | } | ||
| | ||
- | public void sortLexicographically(List< | + | public |
Collections.sort(students, | Collections.sort(students, | ||
} | } | ||
| | ||
- | public void sortByAEM(List< | + | public |
Collections.sort(students, | Collections.sort(students, | ||
public int compare(Student st1, Student st2) { | public int compare(Student st1, Student st2) { | ||
Line 193: | Line 189: | ||
} | } | ||
); | ); | ||
- | } | + | } |
- | + | ||
- | public static String toString(List< | + | |
- | StringBuffer str = new StringBuffer(); | + | |
- | for(Student st : students) | + | |
- | str.append(st+" | + | |
- | str.append(" | + | |
- | return str.toString(); | + | |
- | } | + | |
- | + | ||
- | public static void main(String []args) { | + | |
- | if(args.length == 0) { | + | |
- | System.out.println(" | + | |
- | return; | + | |
- | } | + | |
- | List< | + | |
- | readFromFile(students, | + | |
- | System.out.println(" | + | |
- | System.out.println(toString(students)); | + | |
- | students.get(0).sortLexicographically(students); | + | |
- | System.out.println(" | + | |
- | System.out.println(toString(students)); | + | |
- | students.get(0).sortByAEM(students); | + | |
- | System.out.println(" | + | |
- | System.out.println(toString(students)); | + | |
- | } | + | |
- | | + | |
} | } | ||
</ | </ |
java/anon_inner_classes.1584954941.txt.gz · Last modified: 2020/03/23 09:15 (external edit)