Оцените презентацию от 1 до 5 баллов!
Тип файла:
ppt / pptx (powerpoint)
Всего слайдов:
42 слайда
Для класса:
1,2,3,4,5,6,7,8,9,10,11
Размер файла:
125.35 kB
Просмотров:
61
Скачиваний:
0
Автор:
неизвестен
Слайды и текст к этой презентации:
№1 слайд![Generics](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img0.jpg)
Содержание слайда: Generics
№2 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img1.jpg)
№3 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img2.jpg)
№4 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img3.jpg)
№5 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img4.jpg)
№6 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img5.jpg)
№7 слайд![Generic Types and Methods](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img6.jpg)
Содержание слайда: Generic Types and Methods
There can be:
Generic classes
Generic interfaces
Generic methods
Bounded generic types
Generic wildcards
№8 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img7.jpg)
№9 слайд![Type erasure Java generics](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img8.jpg)
Содержание слайда: Type erasure
Java generics are implemented using type erasure. This means that any specific type information is erased when you compile your code.
№10 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img9.jpg)
№11 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img10.jpg)
№12 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img11.jpg)
№13 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img12.jpg)
№14 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img13.jpg)
№15 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img14.jpg)
№16 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img15.jpg)
№17 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img16.jpg)
№18 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img17.jpg)
№19 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img18.jpg)
№20 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img19.jpg)
№21 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img20.jpg)
№22 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img21.jpg)
№23 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img22.jpg)
№24 слайд![Why Polymorphism doesn t work](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img23.jpg)
Содержание слайда: Why Polymorphism doesn’t work
class Holder<T> {
T[] items;
int num = 0;
void add(T item) { this.items[num++] = item;}
T[] get() { return items; }
}
...
Holder<Integer> h = new Holder<Integer>();
h.add(1);
h.add(2);
Holder<Number> reg = h;
h.add(new Double(1.25));
Integer i3 = h.get()[2]
So Double is a Number but Holder<Double> is not Holder<Number>
№25 слайд![But you can do this This is](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img24.jpg)
Содержание слайда: But you can do this:
This is how you can put different object types in parameterized Holder object:
Holder<Number> h = new Holder<Number>();
h.add(1);
h.add(2);
h.add(new Double(1.25));
Number i3 = h.get()[2]
№26 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img25.jpg)
№27 слайд![Generic Wildcards ? The](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img26.jpg)
Содержание слайда: Generic Wildcards (?)
The wildcard provides a polymorphic - like behavior for declaring generics.
<?> , an unbounded wildcard
<? extends type> , a wildcard with an upper bound
<? super type> , a wildcard with a lower bound
№28 слайд![Unbounded Wildcards The](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img27.jpg)
Содержание слайда: Unbounded Wildcards
The unbounded wildcard represents any data type, similar to the < T > syntax.
public static void printList(List<?> list) {
for(Object x : list) {
System.out.println(x.toString());
}
}
...
ArrayList<String> keywords = new ArrayList<String>();
kyewords.add(“generic”);
printList(keywords);
Use the ? in situations where you do not need a formal parameter type like < T >
№29 слайд![Be careful Holder lt ? gt h](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img28.jpg)
Содержание слайда: Be careful
Holder<?> h = new Holder<String>();
h.add(new Object()); // compile time error
h.add(new String()); // compile time error
// one exception!
h.add(null); // null is member of every type
Working with unbounded wildcards we can only read data, not assign!
№30 слайд![Upper - Bound Wildcards](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img29.jpg)
Содержание слайда: Upper - Bound Wildcards
Bounded wildcards put some restrictions on unknown type:
public static void printList(List<? extends Number> list){
for(Number x : list) {
System.out.println(x.doubleValue());
}
list.add(new Integer(3)); // compile error
}
№31 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img30.jpg)
№32 слайд![Lower Bounded Wildcards a](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img31.jpg)
Содержание слайда: Lower Bounded Wildcards
a lower bounded wildcard restricts the unknown type to be a specific type or a super type of that type
Holder<? super Integer> h = new Holder<Integer>();
h.add(new Integer(1));
Integer i1 = h.get(); // compilation error
// get returns Object
Integer i2 = (Integer)h.get(); // OK
Lower bounded wildcards allow to modify but not read!!
№33 слайд![Example public static void](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img32.jpg)
Содержание слайда: Example
public static void addNumbers(List<? super Integer> list) {
for (int i = 1; i <= 10; i++) {
list.add(i);
}
}
List<Integer> i = new ArrayList<Integer>();
List<Number> n = new ArrayList<Number>();
List<Object> o = new ArrayList<Object>();
addNumbers(i);
addNumbers(n);
addNumbers(o);
This works fine
№34 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img33.jpg)
№35 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img34.jpg)
№36 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img35.jpg)
№37 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img36.jpg)
№38 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img37.jpg)
№39 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img38.jpg)
№40 слайд![Naming Conventions for](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img39.jpg)
Содержание слайда: Naming Conventions for Generics
E for an element
K for a map key
V for a map value
N for a number
T for a generic data type
Use S , U , V , and so on for multiple types in the same class.
№41 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img40.jpg)
№42 слайд![](/documents_6/e8faa57f2c9edb777f6f61ee7678d0c4/img41.jpg)