חלקPart 4: OOP מתקדםAdvanced OOP
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
GenericsGenerics
מחלקות ומתודות גנריות, bounded types, wildcards ו-PECS, ו-type erasure.
Generic classes and methods, bounded types, wildcards and PECS, and type erasure.
הנקודות החשובותKey points
- מחלקה גנרית: מחלקה עם פרמטר טיפוס:generic class: A class with a type parameter:
- פרמטר טיפוס: השם בסוגריים המשולשים (
T,A,B,K,V).type parameter: The name in angle brackets (T,A,B,K,V). Tהוא פרמטר טיפוס.Tis a type parameter.Box<String>מחזירהStringבלי המרה, ו-Box<Integer>מחזירהInteger(וה-autoboxing הופך אותו ל-int).Box<String>returns aStringwith no cast, andBox<Integer>returns anInteger(autoboxing turns it into anint).- בלי generics, מספר היה נכנס לרשימת מחרוזות ומפיל את התוכנית מאוחר יותר.Without generics, a number would get into a list of strings and crash the program later.
- עם
List<String>, javac עוצר מיד, בשורה שבה הטעות.WithList<String>, javac stops right away, on the line with the mistake. lastעובדת לכל רשימה.lastworks for any list.- ב-
max, הגבולT extends Comparable<T>מבטיח שישcompareTo, ולכן אותה מתודה עובדת גם למספרים וגם למחרוזות.Inmax, the boundT extends Comparable<T>guarantees there's acompareTo, so the same method works for numbers and strings alike. sumרק קוראת מספרים, ולכן `?sumonly reads numbers, so `?
מונחיםTerms
- מחלקה גנריתgeneric class: מחלקה עם פרמטר טיפוס:
class Box<T>. כל שימוש קובע מה זה T:Box<String>,Box<Integer>.A class with a type parameter:class Box<T>. Each use decides what T is:Box<String>,Box<Integer>. - פרמטר טיפוסtype parameter: השם בסוגריים המשולשים (
T,A,B,K,V). בתוך המחלקה הוא מתנהג כמו טיפוס רגיל.The name in angle brackets (T,A,B,K,V). Inside the class it acts like a normal type. - מתודה גנריתgeneric method: מתודה עם פרמטר טיפוס משלה, לפני טיפוס ההחזרה:
static <T> T last(List<T> list). javac מסיק את T מהארגומנטים.A method with its own type parameter, before the return type:static <T> T last(List<T> list). javac infers T from the arguments. - טיפוס מוגבלbounded type:
<T extends Comparable<T>>: T יכול להיות רק טיפוס שיודע להשוות את עצמו, ולכן מותר לקרוא ל-compareTo.<T extends Comparable<T>>: T can only be a type that can compare itself, so callingcompareTois allowed. - wildcardwildcard:
?בטיפוס של פרמטר:List<? extends Number>מקבלתList<Integer>וגםList<Double>.?in a parameter's type:List<? extends Number>accepts aList<Integer>and aList<Double>. - PECSPECS: Producer Extends, Consumer Super: רשימה שרק קוראים ממנה היא
? extends T, ורשימה שרק כותבים אליה היא? super T.Producer Extends, Consumer Super: a list you only read from is? extends T, and a list you only write to is? super T. - מחיקת טיפוסיםtype erasure: אחרי הקומפילציה
List<String>ו-List<Integer>הם אותוList. לכן אי אפשרnew T()אוinstanceof List<String>: בזמן ריצה המידע כבר לא קיים.After compiling,List<String>andList<Integer>are the sameList. So you can't donew T()orinstanceof List<String>: at run time the information is gone. - מחלקה גנריתgeneric class: מחלקה עם פרמטר טיפוס:
class Box<T>. כל שימוש קובע מה זה T:Box<String>,Box<Integer>.A class with a type parameter:class Box<T>. Each use decides what T is:Box<String>,Box<Integer>. - פרמטר טיפוסtype parameter: השם בסוגריים המשולשים (
T,A,B,K,V). בתוך המחלקה הוא מתנהג כמו טיפוס רגיל.The name in angle brackets (T,A,B,K,V). Inside the class it acts like a normal type. - מתודה גנריתgeneric method: מתודה עם פרמטר טיפוס משלה, לפני טיפוס ההחזרה:
static <T> T last(List<T> list). javac מסיק את T מהארגומנטים.A method with its own type parameter, before the return type:static <T> T last(List<T> list). javac infers T from the arguments. - טיפוס מוגבלbounded type:
<T extends Comparable<T>>: T יכול להיות רק טיפוס שיודע להשוות את עצמו, ולכן מותר לקרוא ל-compareTo.<T extends Comparable<T>>: T can only be a type that can compare itself, so callingcompareTois allowed. - wildcardwildcard:
?בטיפוס של פרמטר:List<? extends Number>מקבלתList<Integer>וגםList<Double>.?in a parameter's type:List<? extends Number>accepts aList<Integer>and aList<Double>. - PECSPECS: Producer Extends, Consumer Super: רשימה שרק קוראים ממנה היא
? extends T, ורשימה שרק כותבים אליה היא? super T.Producer Extends, Consumer Super: a list you only read from is? extends T, and a list you only write to is? super T. - מחיקת טיפוסיםtype erasure: אחרי הקומפילציה
List<String>ו-List<Integer>הם אותוList. לכן אי אפשרnew T()אוinstanceof List<String>: בזמן ריצה המידע כבר לא קיים.After compiling,List<String>andList<Integer>are the sameList. So you can't donew T()orinstanceof List<String>: at run time the information is gone.
Cheatsheet
Box<T>: מחלקה אחת לכל טיפוסBox<T>: one class for every type
Box<String> name = new Box<>("Dune");
Box<Integer> year = new Box<>(1965);
String n = name.get();
int y = year.get();
System.out.println(n + " " + (y + 1));מתודות גנריות וגבולותGeneric methods and bounds
System.out.println(last(List.of("a", "b", "c")) + " " + max(List.of(3, 9, 4)) + " " + max(List.of("pear", "apple")));wildcards ו-PECSWildcards and PECS
System.out.println(sum(List.of(1, 2, 3)) + " " + sum(List.of(1.5, 2.5)));
List<Number> mixed = new ArrayList<>();
addNumbers(mixed);
mixed.add(0.5);
System.out.println(mixed);שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
incompatible types: int cannot be converted to String | הבדיקה קורית בקומפילציהThe check happens at compile time | incompatible types: int cannot be converted to String: לרשימה של String נכנסות רק מחרוזות.incompatible types: int cannot be converted to String: only strings go into a list of String. |
incompatible types: int cannot be converted to String | צייד באגים: מספר ברשימת מחרוזותBug hunter: a number in a list of strings | incompatible types: int cannot be converted to String: javac שומר שלרשימה ייכנסו רק מחרוזות.incompatible types: int cannot be converted to String: javac makes sure only strings go into the list. |