| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics; | ||
| 2 | + | ||
| 3 | + import java.lang.reflect.Array; | ||
| 4 | + import java.util.Arrays; | ||
| 5 | + | ||
| 6 | + /** | ||
| 7 | + * 对于泛型中创建数组,使用Array.newInstance()是推荐的方式 | ||
| 8 | + */ | ||
| 9 | + public class ArrayMaker<T> { | ||
| 10 | + private Class<T> kind; | ||
| 11 | + | ||
| 12 | + public ArrayMaker(Class<T> kind) { | ||
| 13 | + this.kind = kind; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + @SuppressWarnings("unchecked") | ||
| 17 | + T[] create(int size) { | ||
| 18 | + return (T[]) Array.newInstance(kind, size); | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public static void main(String[] args) { | ||
| 22 | + ArrayMaker<String> stringMaker = | ||
| 23 | + new ArrayMaker<String>(String.class); | ||
| 24 | + String[] stringArray = stringMaker.create(9); | ||
| 25 | + System.out.println(Arrays.toString(stringArray)); | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + /* Output: | ||
| 29 | + [null, null, null, null, null, null, null, null, null] | ||
| 30 | + *///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,118 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics; | ||
| 2 | + | ||
| 3 | + public class BasicBounds { | ||
| 4 | + public static void main(String[] args) { | ||
| 5 | + Solid<Bounded> solid = | ||
| 6 | + new Solid<Bounded>(new Bounded()); | ||
| 7 | + solid.color(); | ||
| 8 | + solid.getY(); | ||
| 9 | + solid.weight(); | ||
| 10 | + } | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + interface HasColor { | ||
| 14 | + java.awt.Color getColor(); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + class Colored<T extends HasColor> { | ||
| 18 | + T item; | ||
| 19 | + | ||
| 20 | + Colored(T item) { | ||
| 21 | + this.item = item; | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + T getItem() { | ||
| 25 | + return item; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + // The bound allows you to call a method: | ||
| 29 | + java.awt.Color color() { | ||
| 30 | + return item.getColor(); | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + class Dimension { | ||
| 35 | + public int x, y, z; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + // This won't work -- class must be first, then interfaces: | ||
| 39 | + // class ColoredDimension<T extends HasColor & Dimension> { | ||
| 40 | + | ||
| 41 | + // Multiple bounds: | ||
| 42 | + class ColoredDimension<T extends Dimension & HasColor> { | ||
| 43 | + T item; | ||
| 44 | + | ||
| 45 | + ColoredDimension(T item) { | ||
| 46 | + this.item = item; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + T getItem() { | ||
| 50 | + return item; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + java.awt.Color color() { | ||
| 54 | + return item.getColor(); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + int getX() { | ||
| 58 | + return item.x; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + int getY() { | ||
| 62 | + return item.y; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + int getZ() { | ||
| 66 | + return item.z; | ||
| 67 | + } | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + interface Weight { | ||
| 71 | + int weight(); | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + // As with inheritance, you can have only one | ||
| 75 | + // concrete class but multiple interfaces: | ||
| 76 | + class Solid<T extends Dimension & HasColor & Weight> { | ||
| 77 | + T item; | ||
| 78 | + | ||
| 79 | + Solid(T item) { | ||
| 80 | + this.item = item; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + T getItem() { | ||
| 84 | + return item; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + java.awt.Color color() { | ||
| 88 | + return item.getColor(); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + int getX() { | ||
| 92 | + return item.x; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + int getY() { | ||
| 96 | + return item.y; | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + int getZ() { | ||
| 100 | + return item.z; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + int weight() { | ||
| 104 | + return item.weight(); | ||
| 105 | + } | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + class Bounded | ||
| 109 | + extends Dimension implements HasColor, Weight { | ||
| 110 | + public java.awt.Color getColor() { | ||
| 111 | + return null; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + public int weight() { | ||
| 115 | + return 0; | ||
| 116 | + } | ||
| 117 | + } | ||
| 118 | + ///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,47 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics; | ||
| 2 | + | ||
| 3 | + import java.util.HashMap; | ||
| 4 | + import java.util.Map; | ||
| 5 | + | ||
| 6 | + /** | ||
| 7 | + * 擦除的补偿 | ||
| 8 | + * 显式传递类型的 Class 对象 | ||
| 9 | + */ | ||
| 10 | + public class ClassTypeCapture<T> { | ||
| 11 | + //类型标签 | ||
| 12 | + Class<T> kind; | ||
| 13 | + | ||
| 14 | + Map<String, Class<?>> types = new HashMap<>(); | ||
| 15 | + | ||
| 16 | + public ClassTypeCapture(Class<T> kind) { | ||
| 17 | + this.kind = kind; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public boolean f(Object arg) { | ||
| 21 | + return kind.isInstance(arg); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + public static void main(String[] args) { | ||
| 25 | + ClassTypeCapture<Building> ctt1 = | ||
| 26 | + new ClassTypeCapture<Building>(Building.class); | ||
| 27 | + System.out.println(ctt1.f(new Building())); | ||
| 28 | + System.out.println(ctt1.f(new House())); | ||
| 29 | + ClassTypeCapture<House> ctt2 = | ||
| 30 | + new ClassTypeCapture<House>(House.class); | ||
| 31 | + System.out.println(ctt2.f(new Building())); | ||
| 32 | + System.out.println(ctt2.f(new House())); | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + class Building { | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + class House extends Building { | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + /* Output: | ||
| 43 | + true | ||
| 44 | + true | ||
| 45 | + false | ||
| 46 | + true | ||
| 47 | + *///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,42 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * 通配符相关类的基础类 | ||
| 5 | + */ | ||
| 6 | + public class CovariantArrays { | ||
| 7 | + public static void main(String[] args) { | ||
| 8 | + Fruit[] fruit = new Apple[10]; | ||
| 9 | + fruit[0] = new Apple(); // OK | ||
| 10 | + fruit[1] = new Jonathan(); // OK | ||
| 11 | + // Runtime type is Apple[], not Fruit[] or Orange[]: | ||
| 12 | + try { | ||
| 13 | + // Compiler allows you to add Fruit: | ||
| 14 | + fruit[0] = new Fruit(); // ArrayStoreException | ||
| 15 | + } catch (Exception e) { | ||
| 16 | + System.out.println(e); | ||
| 17 | + } | ||
| 18 | + try { | ||
| 19 | + // Compiler allows you to add Oranges: | ||
| 20 | + fruit[0] = new Orange(); // ArrayStoreException | ||
| 21 | + } catch (Exception e) { | ||
| 22 | + System.out.println(e); | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + class Fruit { | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + class Apple extends Fruit { | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + class Jonathan extends Apple { | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + class Orange extends Fruit { | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + /* Output: | ||
| 40 | + java.lang.ArrayStoreException: Fruit | ||
| 41 | + java.lang.ArrayStoreException: Orange | ||
| 42 | + *///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,38 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * 末班方法设计模式 | ||
| 5 | + */ | ||
| 6 | + public class CreatorGeneric { | ||
| 7 | + public static void main(String[] args) { | ||
| 8 | + Creator c = new Creator(); | ||
| 9 | + c.f(); | ||
| 10 | + } | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + abstract class GenericWithCreate<T> { | ||
| 14 | + final T element; | ||
| 15 | + | ||
| 16 | + GenericWithCreate() { | ||
| 17 | + element = create(); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + abstract T create(); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + class X { | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + class Creator extends GenericWithCreate<X> { | ||
| 27 | + X create() { | ||
| 28 | + return new X(); | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + void f() { | ||
| 32 | + System.out.println(element.getClass().getSimpleName()); | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + /* Output: | ||
| 37 | + X | ||
| 38 | + *///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics; | ||
| 2 | + | ||
| 3 | + import java.util.ArrayList; | ||
| 4 | + | ||
| 5 | + public class ErasedTypeEquivalence { | ||
| 6 | + public static void main(String[] args) { | ||
| 7 | + Class c1 = new ArrayList<String>().getClass(); | ||
| 8 | + Class c2 = new ArrayList<Integer>().getClass(); | ||
| 9 | + Class c3 = ArrayList.class; | ||
| 10 | + System.out.println(c1 == c2); | ||
| 11 | + System.out.println(c1 == c3); | ||
| 12 | + } | ||
| 13 | + } | ||
| 14 | + /* Output: | ||
| 15 | + true | ||
| 16 | + true | ||
| 17 | + *///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,39 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics;//: generics/FactoryConstraint.java | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * 显式工厂 | ||
| 5 | + */ | ||
| 6 | + public class FactoryConstraint { | ||
| 7 | + public static void main(String[] args) { | ||
| 8 | + new Foo2<Integer>(new IntegerFactory()); | ||
| 9 | + new Foo2<Widget>(new Widget.Factory()); | ||
| 10 | + } | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + interface FactoryI<T> { | ||
| 14 | + T create(); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + class Foo2<T> { | ||
| 18 | + private T x; | ||
| 19 | + | ||
| 20 | + public <F extends FactoryI<T>> Foo2(F factory) { | ||
| 21 | + x = factory.create(); | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + class IntegerFactory implements FactoryI<Integer> { | ||
| 27 | + public Integer create() { | ||
| 28 | + return 0; | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + class Widget { | ||
| 33 | + public static class Factory implements FactoryI<Widget> { | ||
| 34 | + public Widget create() { | ||
| 35 | + return new Widget(); | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + ///:~ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + package com.brianway.learning.java.base.generics;//: generics/GenericArray.java | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * 泛型数组 | ||
| 5 | + */ | ||
| 6 | + public class GenericArray<T> { | ||
| 7 | + private T[] array; | ||
| 8 | + | ||
| 9 | + @SuppressWarnings("unchecked") | ||
| 10 | + public GenericArray(int sz) { | ||
| 11 | + array = (T[]) new Object[sz]; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + public void put(int index, T item) { | ||
| 15 | + array[index] = item; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + public T get(int index) { | ||
| 19 | + return array[index]; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + // Method that exposes the underlying representation: | ||
| 23 | + public T[] rep() { | ||
| 24 | + return array; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + public static void main(String[] args) { | ||
| 28 | + GenericArray<Integer> gai = | ||
| 29 | + new GenericArray<Integer>(10); | ||
| 30 | + // This causes a ClassCastException: | ||
| 31 | + //! Integer[] ia = gai.rep(); | ||
| 32 | + // This is OK: | ||
| 33 | + Object[] oa = gai.rep(); | ||
| 34 | + } | ||
| 35 | + } ///:~ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments