נבנה על ידיBuilt by Ariel Yohimovich

JavaJava
22
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובות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 הוא פרמטר טיפוס.T is a type parameter.
  • Box<String> מחזירה String בלי המרה, ו-Box<Integer> מחזירה Integer (וה-autoboxing הופך אותו ל-int).Box<String> returns a String with no cast, and Box<Integer> returns an Integer (autoboxing turns it into an int).
  • בלי generics, מספר היה נכנס לרשימת מחרוזות ומפיל את התוכנית מאוחר יותר.Without generics, a number would get into a list of strings and crash the program later.
  • עם List<String>, javac עוצר מיד, בשורה שבה הטעות.With List<String>, javac stops right away, on the line with the mistake.
  • last עובדת לכל רשימה.last works for any list.
  • ב-max, הגבול T extends Comparable<T> מבטיח שיש compareTo, ולכן אותה מתודה עובדת גם למספרים וגם למחרוזות.In max, the bound T extends Comparable<T> guarantees there's a compareTo, so the same method works for numbers and strings alike.
  • sum רק קוראת מספרים, ולכן `?sum only 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 calling compareTo is allowed.
  • wildcardwildcard: ? בטיפוס של פרמטר: List<? extends Number> מקבלת List<Integer> וגם List<Double>.? in a parameter's type: List<? extends Number> accepts a List<Integer> and a List<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> and List<Integer> are the same List. So you can't do new T() or instanceof 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 calling compareTo is allowed.
  • wildcardwildcard: ? בטיפוס של פרמטר: List<? extends Number> מקבלת List<Integer> וגם List<Double>.? in a parameter's type: List<? extends Number> accepts a List<Integer> and a List<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> and List<Integer> are the same List. So you can't do new T() or instanceof 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 timeincompatible 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 stringsincompatible 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.

נבנה על ידיBuilt by Ariel Yohimovich