标签:c# time() tac from 字母 静态 font 动作 添加
public interface java.lang.reflect.ParameterizedType extends Type
public interface java.lang.reflect.GenericDeclaration
所有已知实现类:Class、Constructor、Method
public class ArrayList{
public Object get(int i){......}
public void add(Object o){......}
......
private Object[] elementData;
}
/**jdk1.5之前的写法,容易出问题*/
ArrayList arrayList1=new ArrayList();
arrayList1.add(1);
arrayList1.add(1L);
arrayList1.add("asa");
int i=(Integer) arrayList1.get(1);//因为不知道取出来的值的类型,类型转换的时候容易出错
/** jdk1.5之后加入泛型*/
ArrayList<String> arrayList2=new ArrayList<String>(); //限定数组列表中的类型
//arrayList2.add(1); //因为限定了类型,所以不能添加整形
//arrayList2.add(1L);//因为限定了类型,所以不能添加整长形
arrayList2.add("asa");//只能添加字符串
String str=arrayList2.get(0);//因为知道取出来的值的类型,所以不需要进行强制类型转换
public class HashMap<K,V> {
public V put(K key, V value) {...}
public V get(Object key) {...}
...
}
public class Pair<T> {
private T value;
public Pair(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
public static void main(String[] args) throws ClassNotFoundException {
Pair<String> pair = new Pair<String>("Hello");//注意,"="号左边和右边都要使用<>指定泛型的实际类型
String str = pair.getValue();
pair.setValue("World");
}
class Pair<T, S, P, U, E> { }
interface Show<T,U>{
void show(T t,U u);
}
public class ShowTest implements Show<String, Date> {
@Override
public void show(String t, Date u) {
System.out.println(t + " " + u.getTime());
}
}
Show<String, Date> show = new ShowTest();
show.show("包青天", new Date());
class Person<S> {
public <W> void show(W w) {//这里的【W】完全等价于Object
if (w != null) System.out.println(w.toString());
}
public static <Y> void staticShow(Y y) {
if (y != null) System.out.println(y.toString());
//静态方法不能访问在类声明上定义的类型变量
//S s;//错误提示:Cannot make a static reference to the non-static type S
}
}
public static <T extends Comparable> T get(T t1,T t2) //继承或实现都用extends
public static <T extends Comparable & Serializable> T get(T t1,T t2) //使用 & 符号给出多个限定
public static <T extends Object & Comparable & Serializable> T get(T t1,T t2) //继承的类Object必须放在首位
public static void main(String[] args) throws Exception {
List<Integer> listInteger = new ArrayList<Integer>();
printCollection(listInteger);//报错 The method printCollection(Collection<Object>) in the type Test is not applicable for the arguments (List<Integer>)
}
public static void printCollection(Collection<Object> collection) {
for (Object obj : collection) {
System.out.println(obj);
}
}
public static void printCollection(Collection<?> collection) {...}
collection.add(new Object());//The method add(capture#1-of ?) in the type Collection<capture#1-of ?> is not applicable for the arguments (Object)
List<? extends Number> x = new ArrayList<Integer>();//正确
List<? extends Number> y = new ArrayList<Object>();//错误 Type mismatch: cannot convert from ArrayList<Object> to List<? extends Number>
List<? super Number> y = new ArrayList<Object>();//正确
List<? super Number> x = new ArrayList<Integer>();//错误 Type mismatch: cannot convert from ArrayList<Integer> to List<? super Number>
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
System.out.println((list1.getClass() == list2.getClass()) + " " + (list1.getClass() == ArrayList.class));//true true
List<Integer> list = new ArrayList<Integer>();
list.add(10086);
Method method = list.getClass().getMethod("add", Object.class);
//运行时利用反射机制调用集合的add方法,跳过编译时的泛型检查
method.invoke(list, "虽然集合中对元素限定的泛型是Integer,但是也能通过反射把字符串添加到集合中");
Object object = list.get(1);
System.out.println(object.getClass().getSimpleName() + " " + (object.getClass() == String.class));//String true
try {
System.out.println(((Object) list.get(1)).getClass());//class java.lang.String
System.out.println(list.get(1).getClass());//如果不指定list.get(1)的类型,则会默认将其强制转换为集合上指定的泛型类型
} catch (Exception e) {
e.printStackTrace();//java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
}
class Pair<T> {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
class Pair {
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
public class Pair<T extends Comparable& Serializable> { ... }
public class Pair<T extends Serializable & Comparable>
public class Test {
public static void main(String[] args) {
/**不指定泛型的时候,泛型变量的类型为 该方法中的几种类型的同一个父类的最小级,直到Object*/
int i = Test.add(1, 2); //这两个参数都是Integer,所以T为Integer类型
Number f = Test.add(1, 1.2);//这两个参数一个是Integer,一个是Float,所以取同一父类的最小级,为Number
Object o = Test.add(1, "asd");//这两个参数一个是Integer,一个是Float,所以取同一父类的最小级,为Object
/**指定泛型的时候,该方法中的几种类型必须是该泛型实例类型或者其子类*/
int a = Test.<Integer> add(1, 2);//指定了Integer,所以只能为Integer类型或者其子类
//int b = Test.<Integer> add(1, 2.2);//编译错误,指定了Integer,不能为Float
Number c = Test.<Number> add(1, 2.2); //指定为Number,所以可以为Integer和Float
}
public static <T> T add(T x, T y) {
return y;
}
}
public interface java.lang.reflect.GenericDeclaration
public static <T extends Person, U> void main(String[] args) throws Exception {
Method method = Test.class.getMethod("main", String[].class);
TypeVariable<?>[] tvs = method.getTypeParameters();//返回声明顺序的 TypeVariable 对象的数组
System.out.println("声明的类型变量有:" + Arrays.toString(tvs));//[T, U]
for (int i = 0; i < tvs.length; i++) {
GenericDeclaration gd = tvs[i].getGenericDeclaration();
System.out.println("【GenericDeclaration】" + gd);//public static void com.bqt.Test.main(java.lang.String[]) throws java.lang.Exception
System.out.println(gd.getTypeParameters()[i] == tvs[i]);//true。 GenericDeclaration和TypeVariable两者相互持有对方的引用
System.out.println(tvs[i] + " " + tvs[i].getName() + " " + Arrays.toString(tvs[i].getBounds()));//T T [class com.bqt.Person] 和 U U [class java.lang.Object]
}
}
标签:c# time() tac from 字母 静态 font 动作 添加
原文地址:http://www.cnblogs.com/baiqiantao/p/7475696.html