边界处的动作
public class ArrayMaker {
private Class tClass;
public ArrayMaker(Class tClass) {
this.tClass = tClass;
}
public T[] create(int size){
return (T[])Array.newInstance(tClass,size);
}
public static void main(String[] args) {
ArrayMaker arrayMaker=new ArrayMaker<>(String.class);
String[] strings = arrayMaker.create(9);
System.out.println(Arrays.toString(strings));
}
}
//运行结果为
[null, null, null, null, null, null, null, null, null]
public class ArrayMaker1 {
List create(){
return new ArrayList();
}
public static void main(String[] args) {
ArrayMaker1 maker1 = new ArrayMaker1<>();
List strings = maker1.create();
System.out.println(strings);
}
}
//运行结果
[]
public class ArrayMark2 {
List create(T t,int size){
List list=new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(t);
}
return list;
}
public static void main(String[] args) {
ArrayMark2 arrayMark2=new ArrayMark2<>();
List list = arrayMark2.create("hello", 3);
System.out.println(list);
}
}
//运行结果为
[hello, hello, hello]
public class SimpleHolder {
private Object obj;
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public static void main(String[] args) {
SimpleHolder holder = new SimpleHolder();
holder.setObj("Item");
String obj = (String) holder.getObj();
}
}
public java.lang.Object getObj();
Code:
0: aload_0
1: getfield #2 // Field obj:Ljava/lang/Object;
4: areturn
public void setObj(java.lang.Object);
Code:
0: aload_0
1: aload_1
2: putfield #2 // Field obj:Ljava/lang/Object;
5: return
public static void main(java.lang.String[]);
Code:
0: new #3 // class generic/SimpleHolder
3: dup
4: invokespecial #4 // Method "":()V
7: astore_1
8: aload_1
9: ldc #5 // String Item
11: invokevirtual #6 // Method setObj:(Ljava/lang/Object;)V
14: aload_1
15: invokevirtual #7 // Method getObj:()Ljava/lang/Object;
18: checkcast #8 // class java/lang/String
21: astore_2
22: return
public class SimpleHolder {
private T obj;
public T getObj() {
return obj;
}
public void setObj(T obj) {
this.obj = obj;
}
public static void main(String[] args) {
SimpleHolder holder = new SimpleHolder();
holder.setObj("Item");
String obj = holder.getObj();
}
}
//从 getObj() 返回之后的转型消失了, 但是我们还知道传递给setObj() 的值在编译器会接受检查,下面是相关字节码。
public T getObj();
Code:
0: aload_0
1: getfield #2 // Field obj:Ljava/lang/Object;
4: areturn
public void setObj(T);
Code:
0: aload_0
1: aload_1
2: putfield #2 // Field obj:Ljava/lang/Object;
5: return
public static void main(java.lang.String[]);
Code:
0: new #3 // class generic/GenericHolder
3: dup
4: invokespecial #4 // Method "":()V
7: astore_1
8: aload_1
9: ldc #5 // String item
11: invokevirtual #6 // Method setObj:(Ljava/lang/Object;)V
14: aload_1
15: invokevirtual #7 // Method getObj:()Ljava/lang/Object;
18: checkcast #8 // class java/lang/String
21: astore_2
22: return
public class Erased {
private final int SIZE=100;
static void f(Object obj){
//下面这段代码是错误的
if (obj instanceof T){
T t=new T();
T [] array=new T[SIZE];
T[] arrays=(T)Object[SIZE]; //未经检查的警告
}
}
}
class Building{}
class House extends Building{}
public class Erased {
Class tClass;
public Erased(Class tClass) {
this.tClass = tClass;
}
boolean f(Object obj){
return tClass.isInstance(obj);
}
public static void main(String[] args) {
//1, 创建泛型 为 Building类
Erased erased=new Erased<>(Building.class);
System.out.println(erased.f(new House()));
System.out.println(erased.f(new Building()));
//2,创建泛型为 House类
Erased erased1=new Erased<>(House.class);
System.out.println(erased1.f(new Building()));
System.out.println(erased1.f(new House()));
}
}
//运行结果为
true
true
false
true
public class ClassAsFactory {
T t;
public ClassAsFactory(Class tClass) {
try {
t = tClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class Employee{}
class InstantiateGenericType{
public static void main(String[] args) {
ClassAsFactory employeeClassAsFactory=new ClassAsFactory<>(Employee.class);
System.out.println("ClassAsFactory succeeded");
try {
ClassAsFactory classAsFactory=new ClassAsFactory<>(Integer.class);
}catch (Exception e){
System.out.println(" ClassAsFactory failed");
}
}
}
//运行结果为
ClassAsFactory succeeded
ClassAsFactory failed
interface Factory{
T create();
}
//创建对象工厂
class Foo2{
private T t;
public > Foo2(F factory){
t=factory.create();
}
}
class IntegerFactory implements Factory{
@Override
public Integer create() {
return new Integer(0);
}
}
class Widget{
static class TestFactory implements Factory{
@Override
public Widget create() {
return new Widget();
}
}
}
public class FactoryConstraint {
public static void main(String[] args) {
new Foo2<>(new IntegerFactory());
new Foo2<>(new Widget.TestFactory());
}
}
public abstract class GenericWithCreate {
final T element;
GenericWithCreate() {
this.element = create();
}
abstract T create();
}
class X{}
class Creator extends GenericWithCreate{
@Override
X create() {
return new X();
}
void f(){
System.out.println(element.getClass().getSimpleName());
}
}
class CreatorGeneric{
public static void main(String[] args) {
Creator creator = new Creator();
creator.f();
}
}
//运行结果为
X
public class ListOfGenerics {
private List lists=new ArrayList<>();
void add(T t){
lists.add(t);
}
T get(int index){
return lists.get(index);
}
}
class Generic{}
class ArrayOfGenericReference{
static Generic [] gia;
}
public class ArrayOfGeneric {
static final int SIZE = 100;
static Generic[] gia;
public static void main(String[] args) {
//compiles produces classCastException
//编译产生 classCastException
//gia=(Generic[])new Object[SIZE];
//运行时类型是原始(擦除)类型
gia = (Generic[]) new Generic[SIZE];
System.out.println(gia.getClass().getSimpleName());
gia[0] = new Generic();
//编译时错误
// gia[1]=new Object();
//在编译时发现类型不匹配
//gia[2]=new Generic();
}
}
//运行结果为
Generic[]
public class GenericArray {
private T [] array;
public GenericArray(int size) {
array= (T[]) new Object[size];
}
public void put(int index,T item){
array[index] = item;
}
public T get(int index){
return array[index];
}
public T[] rep(){
return array;
}
public static void main(String[] args) {
GenericArray genericArray =new GenericArray<>(5);
//ClassCastException
//Integer [] integers=genericArray.rep();
//下面这个是没问题的
java.lang.Object [] objects=genericArray.rep();
}
}
public class GenericArray2 {
private Object[] array;
public GenericArray2(int size) {
array=new Object[size];
}
public void put(int index,T item){
array[index] =item;
}
public T get(int index){
return (T) array[index];
}
public T[] rep(){
return (T[]) array;
}
public static void main(String[] args) {
GenericArray2 integerGenericArray2=new GenericArray2<>(5);
for (int i = 0; i < 5; i++) {
integerGenericArray2.put(i,i);
}
for (int i = 0; i < 5; i++) {
Integer integer = integerGenericArray2.get(i);
System.out.println(integer);
}
try {
Integer[] integers = integerGenericArray2.rep();
}catch (Exception e){
System.out.println("异常了");
}
}
}
//运行结果为
0
1
2
3
4
异常了
public class GenericArrayWithTypeToken {
private T[] array;
public GenericArrayWithTypeToken(Class tClass,int size){
array= (T[]) Array.newInstance(tClass,size);
}
public void put(int index,T item){
array[index]=item;
}
public T get(int index){
return array[index];
}
public T[] rep(){
return array;
}
public static void main(String[] args) {
GenericArrayWithTypeToken integers=new GenericArrayWithTypeToken<>(Integer.class,5);
Integer[] rep = integers.rep();
//这个是没有错误的
}
}