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

חזרה למפגשBack
JavaJava

GenericsGenerics

מפגש 22: מחלקות ומתודות גנריות, bounded types, wildcards ו-PECS, ו-type erasure.Session 22: Generic classes and methods, bounded types, wildcards and PECS, and type erasure.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • Box<T>: מחלקה אחת לכל טיפוסBox<T>: one class for every type
  • הבדיקה קורית בקומפילציהThe check happens at compile time
  • מתודות גנריות וגבולותGeneric methods and bounds
  • wildcards ו-PECSWildcards and PECS
  • טעויות נפוצותCommon mistakes
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • מחלקה גנרית: מחלקה עם פרמטר טיפוס: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).
  • מתודה גנרית: מתודה עם פרמטר טיפוס משלה, לפני טיפוס ההחזרה:generic method: A method with its own type parameter, before the return type:
  • טיפוס מוגבל: <T extends Comparable<T>>:bounded type: <T extends Comparable<T>>:
  • wildcard: ? בטיפוס של פרמטר:wildcard: ? in a parameter's type:
  • PECS: Producer Extends, Consumer Super:PECS: Producer Extends, Consumer Super:
נושאTopic 2

Box<T>: מחלקה אחת לכל טיפוסBox<T>: one class for every type

Box<T>: מחלקה אחת לכל טיפוסBox<T>: one class for every type

  • 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).
נושאTopic 3

הבדיקה קורית בקומפילציהThe check happens at compile time

הבדיקה קורית בקומפילציהThe check happens at compile time

  • בלי 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.
נושאTopic 4

מתודות גנריות וגבולותGeneric methods and bounds

מתודות גנריות וגבולותGeneric methods and bounds

  • 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.
נושאTopic 5

wildcards ו-PECSWildcards and PECS

wildcards ו-PECSWildcards and PECS

  • sum רק קוראת מספרים, ולכן `?sum only reads numbers, so `?
  • extends Number (מקבלת List<Integer> ו-List<Double>`).extends Number (it accepts List<Integer> and List<Double>`).
  • addNumbers רק כותבת Integer, ולכן `?addNumbers only writes Integers, so `?
נושאTopic 6

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • `incompatible types:`incompatible types:
נושאTopic 7

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 22: Shelf<T> גנריShelf, step 22: a generic Shelf<T>

מחלקה גנרית: מחלקה עם פרמטר טיפוס: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).

מתודה גנרית: מתודה עם פרמטר טיפוס משלה, לפני טיפוס ההחזרה:generic method: A method with its own type parameter, before the return type:

טיפוס מוגבל: <T extends Comparable<T>>:bounded type: <T extends Comparable<T>>:

wildcard: ? בטיפוס של פרמטר:wildcard: ? in a parameter's type:

PECS: Producer Extends, Consumer Super:PECS: Producer Extends, Consumer Super:

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 `?

extends Number (מקבלת List<Integer> ו-List<Double>`).extends Number (it accepts List<Integer> and List<Double>`).

addNumbers רק כותבת Integer, ולכן `?addNumbers only writes Integers, so `?

`incompatible types:`incompatible types:

מדף, צעד 22: Shelf<T> גנריShelf, step 22: a generic Shelf<T>

מה מחזירה get() של Box<String>?What does get() of a Box<String> return?

  • Object, וצריך המרהObject, and you need a cast
  • StringString
  • TT
  • BoxBox

StringString

למה List<int> לא מתקמפל?Why doesn't List<int> compile?

  • כי פרמטר טיפוס חייב להיות טיפוס של עצם, כמו IntegerBecause a type parameter must be an object type, like Integer
  • כי int איטיBecause int is slow
  • כי צריך List<Number>Because you need List<Number>
  • זה מתקמפלIt compiles

כי פרמטר טיפוס חייב להיות טיפוס של עצם, כמו IntegerBecause a type parameter must be an object type, like Integer

מתודה רק מוסיפה מספרים שלמים לרשימה. איזה טיפוס פרמטר הכי גמיש?A method only adds whole numbers to a list. Which parameter type is most flexible?

  • List<? super Integer>List<? super Integer>
  • List<? extends Integer>List<? extends Integer>
  • List<Integer> בלבדOnly List<Integer>
  • List<?>List<?>

List<? super Integer>List<? super Integer>

זוגA pair

  • class Pair<A, B> עם first(), second(), swap() שמחזירה Pair<B, A> חדש, ו-toString (Dune, 1965).class Pair<A, B> with first(), second(), swap() returning a new Pair<B, A>, and a toString (Dune, 1965).

ספירה גנריתA generic count

  • static <T> int count(List<T> list, T wanted) סופרת כמה איברים שווים (equals) ל-wanted.static <T> int count(List<T> list, T wanted) counts how many items equal (equals) wanted.

מינימום לכל טיפוסA minimum for any type

  • static <T extends Comparable<T>> T min(List<T> list).static <T extends Comparable<T>> T min(List<T> list).

צייד באגים: מספר ברשימת מחרוזותBug hunter: a number in a list of strings

  • התוכנית לא מתקמפלת.The program doesn't compile.

ממוצע לכל מספרAn average of any numbers

  • `static double average(List<?`static double average(List<?

מה למדנוWhat we learned

  • מחלקה גנרית: מחלקה עם פרמטר טיפוס:generic class: A class with a type parameter:
  • T הוא פרמטר טיפוס.T is a type parameter.
  • בלי generics, מספר היה נכנס לרשימת מחרוזות ומפיל את התוכנית מאוחר יותר.Without generics, a number would get into a list of strings and crash the program later.
  • last עובדת לכל רשימה.last works for any list.
  • sum רק קוראת מספרים, ולכן `?sum only reads numbers, so `?
  • `incompatible types:`incompatible types:

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

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