Содержание
- 2. What if you could say that your code works with "some unspecified type," rather than a
- 3. Generics implement the concept of parameterized types, which allow multiple types.
- 4. A class that holds a single object class Automobile {} public class Holder1 { private Automobile
- 5. A class that holds an Object public class Holder2 { private Object a; public Holder2(Object a)
- 6. Simple generic class public class Holder3 { private T a; public Holder3(T a) { this.a =
- 7. Generic Types and Methods There can be: Generic classes Generic interfaces Generic methods Bounded generic types
- 8. The core idea of Java generics: You tell it what type you want to use, and
- 9. Type erasure Java generics are implemented using type erasure. This means that any specific type information
- 10. How type erasure works? public class Holder3 { private T a; public Holder3(T a) { this.a
- 11. Compensating for erasure public class Erased { private final int SIZE = 100; public static void
- 12. Generic class with two types public class TwoTuple { public final A first; public final B
- 13. You cannot use primitives as type parameters! TwoTuple ttsi = new TwoTuple ("hi", 47); but not
- 14. Inheritance with generic classes public class ThreeTuple extends TwoTuple { public final C third; public ThreeTuple(A
- 15. Generic interface public interface Generator { T next(); } public class Fibonacci implements Generator { private
- 16. Generic Methods public class GenericMethods { public void f(T x) { System.out.println(x.getClass().getName()); } public static void
- 17. The Syntax for Invoking a Generic Method Generics have an optional syntax for specifying the type
- 18. Leveraging type argument inference public class Tuple { public static TwoTuple tuple(A a, B b) {
- 19. Anonymous inner classes public interface Generator { T next(); } class Customer { private Customer() {}
- 20. Bounded Generic Types Because erasure removes type information, the only methods you can call for an
- 21. Compound bounds class Dimension { public int x, y, z; } // Multiple bounds: class ColoredDimension
- 22. Bounds and Inheritance class HoldItem { T item; HoldItem(T item) { this.item = item; } T
- 23. Polymorphism and Generics class Holder { T item; void set(T item) { this.item = item; }
- 24. Why Polymorphism doesn’t work class Holder { T[] items; int num = 0; void add(T item)
- 25. But you can do this: This is how you can put different object types in parameterized
- 26. More Example abstract class Animal { public abstract void check(); } class Dog extends Animal {
- 27. Generic Wildcards (?) The wildcard provides a polymorphic - like behavior for declaring generics. , an
- 28. Unbounded Wildcards The unbounded wildcard represents any data type, similar to the syntax. public static void
- 29. Be careful Holder h = new Holder (); h.add(new Object()); // compile time error h.add(new String());
- 30. Upper - Bound Wildcards Bounded wildcards put some restrictions on unknown type: public static void printList(List
- 31. More example class AnimalDoctor { void checkAnimal(Holder animal) { animal.get().check(); // OK animal.set(new Cat()); // Error:
- 32. Lower Bounded Wildcards a lower bounded wildcard restricts the unknown type to be a specific type
- 33. Example public static void addNumbers(List list) { for (int i = 1; i list.add(i); } }
- 34. More example class AnimalDoctor { void checkAnimal(Holder dog) { animal.get().check(); // Error! // get() returns Object
- 35. What's the difference? void check(Holder holder) { } void check(Holder holder) { }
- 36. There IS a huge difference! class Main { void check(Holder obj) { obj.set(new Dog()); obj.set(new Cat());
- 37. There IS a huge difference! class Main { void check(Holder obj) { obj.set(new Dog()); // Error
- 38. Which will compile? 1) List list = new ArrayList (); 2) List aList = new ArrayList
- 39. Which will compile? 1) List list = new ArrayList (); 2) List aList = new ArrayList
- 40. Naming Conventions for Generics E for an element K for a map key V for a
- 41. Summary 1) Generics let you enforce compile-time type safety. Holder h1 = new Holder (); Holder
- 43. Скачать презентацию