java泛型擦除的神秘之处,边界处的动作,擦除的补偿

java泛型擦除的神秘之处

package org.rui.generics.erasure;

public class HasF {
	public void f(){
		System.out.println("hasf.f");
	}

}

package org.rui.generics.erasure;
/**
 * 边界 声明T必须具有类型HasF或者从Hasf导出的类型。
 * 如果情况确实如此,那么就可以安全地在obj上调用f()了
 * T擦除了 HasF
 * @author lenovo
 *
 * @param 
 */
//class Manipulator  Error: 不能调用obj.f()
 class Manipulator 
 {
	private T obj;
	public Manipulator(T x){obj=x;}
	public void manipulate(){obj.f();}
	//获取泛型类型
	public T getGenType(){return obj;}
	
	
}


public class Manipulation 
{
	public static void main(String[] args) 
	{
		HasF h=new HasF();
		Manipulator man=new Manipulator(h);
		man.manipulate();
		System.out.println("genType:"+man.getGenType().getClass());
	}
}
/**output:
hasf.f
genType:class org.rui.generics.erasure.HasF
*/

package org.rui.generics.erasure;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * 边界处的动作
 * 即失kind被存储为Class 擦除也意味着它实际将被存储为 Class,没有任何参数
 * @author lenovo
 *
 */
public class ArrayMaker {
	private Class kind;
	public ArrayMaker(Class kind)
	{
		this.kind=kind;
	}
	
	@SuppressWarnings("unchecked")
	T[] create(int size)
	{
		return (T[])Array.newInstance(kind, size);
	}
	
	public static void main(String[] args) 
	{
		ArrayMaker maker=new ArrayMaker(String.class);
		String[] stringArray=maker.create(9);
		System.out.println(Arrays.toString(stringArray));
	}
}
/**output:
 * [null, null, null, null, null, null, null, null, null]
 */

package org.rui.generics.erasure;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 边界处的动作
 * 容器而不是数组 情况不不同了
 * @author lenovo
 *
 */
public class ListMaker {
	/*private Class kind;
	public ListMaker(Class kind)
	{
		this.kind=kind;
	}*/
	

	List create(T t,int n)
	{
		List list= new ArrayList();
		for(int i=0;i maker=new ListMaker();
		List str=maker.create("hello",4);
		System.out.println(str);
	}
}

package org.rui.generics.erasure;
/**
 * 擦除的补偿
 * 
 * 编译器将确保类型标签可以匹配泛型参数
 * @author lenovo
 *
 */

class Building{}
class House extends Building{}

public class ClassTypeCapture {
	
	Class kind;
	public ClassTypeCapture(Class kind)
	{
		this.kind=kind;
	}
	
	public boolean f(Object obj)
	{
		System.out.println(kind +"   isInstance    "+obj);
		return kind.isInstance(obj);
	}
	
	public static void main(String[] args)
	{
		ClassTypeCapture ctc=
				new ClassTypeCapture(Building.class);
		System.out.println(ctc.f(new Building()));
		//父类  与子对比
		System.out.println(ctc.f(new House()));
		
		ClassTypeCapture ctc2=
				new ClassTypeCapture(House.class);	
		//House is building 子对比父=false
		System.out.println(ctc2.f(new Building()));
		System.out.println(ctc2.f(new House()));
		
	}

}
/**
output:
true
true
false
true
*/

package org.rui.generics.erasure;
/**
 * 创建类型实例
 * 
 * @author lenovo
 *
 */

class ClassAsFactory
{
	 T x;
	 public ClassAsFactory(Class kind)
	 {
		 try {
			x=kind.newInstance();
		} catch (Exception e) 
		{
			e.printStackTrace();
		} 
	 }
}

////////////////////////////////////
class Employee{}

public class InstantiateGenericType {
	public static void main(String[] args) 
	{
		ClassAsFactory caf=
				new ClassAsFactory(Employee.class);
		System.out.println("caf:"+caf.x);
		
		/*try {
		 //Integer 没有默认的构造器
			ClassAsFactory cafInt=
					new ClassAsFactory(Integer.class);
		} catch (Exception e) {
			System.out.println("ClassAsFactory failed");
		}*/
	
		
		
	}
	

}

package org.rui.generics.erasure;

interface FactoryI
{
	T create();
}

class Foo2
{
	private T x;
	public > Foo2(F f)
	{
		x=f.create();
	}
}

///////////////////////////////////////////////////////////////////
class IntegerFactory implements FactoryI
{
	public Integer create() 
	{
		return new Integer(0);
	}
	
}
///////////////////////////////////////////////////////////////////
class Widget
{
  public static class Factory implements FactoryI
  {
		public Widget create()
		{
			return new Widget();
		}
   }	
}
///////////////////////////////////////////////////////////////////
public class FactoryConstraint {
	public static void main(String[] args) 
	{
		new Foo2(new IntegerFactory());
		new Foo2(new Widget.Factory());
	}
}

package org.rui.generics.erasure;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Frob{}
class Fnorkle{}
class Quark{}
class Particle{};
/**
 * java 泛型是使用擦除来实现的
 * 在泛型代码内部,无法获得任何有关泛型 参数类型信息
 * @author lenovo
 *
 */

public class LostInformation {
	 public static void main(String[] args)
	 {
		 List list=new ArrayList();
		 Map map=new HashMap();
		 Quark quark=new Quark();
		 Particle p=new Particle();
		 
		 System.out.println(Arrays.toString(
				 list.getClass().getTypeParameters()));
		 
		 System.out.println(Arrays.toString(
				 map.getClass().getTypeParameters()));
		 
		 System.out.println(Arrays.toString(
				 quark.getClass().getTypeParameters()));
		 
		 System.out.println(Arrays.toString(
				 p.getClass().getTypeParameters()));
	 }
}
/*output:
[E]
[K, V]
[Q]
[POSITION, MOMENTUM]
*/


你可能感兴趣的:(java,J2SE)