标签:cab compareto mis run 参考 效果 引用传递 避免 instead
public static void main(String[] args) {
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("123");
arrayList.add(123);//编译错误
}
//以前的写法:
ArrayList arrayList = new ArrayList();
//现在的写法:
ArrayList<String> arrayList = new ArrayList<String>();
ArrayList<String> arrayList1 = new ArrayList(); //可以实现与完全【使用泛型参数】一样的效果
arrayList1.add("1");//编译通过
arrayList1.add(1);//编译错误
String str = arrayList1.get(0);//返回类型就是String
ArrayList arrayList2 = new ArrayList<String>();//可以实现与完全【不使用泛型参数】一样的效果
arrayList2.add(1);//编译通过
Object object = arrayList2.get(0);//返回类型就是Object
new ArrayList<String>().add("11");//编译通过
new ArrayList<String>().add(22);//编译错误
String string = new ArrayList<String>().get(0);//返回类型就是String
Type safety: The expression of type ArrayList needs unchecked conversion to conform to ArrayList<String> .
类型安全性:ArrayList类型的表达式需要未经检查的转换才能符合ArrayList <String>
ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized .
ArrayList是一个原始类型。 对泛型类型ArrayList <E>的引用应该被参数化
ArrayList<String> arrayList=new ArrayList<Object>();//编译错误 Type mismatch: cannot convert from ArrayList<Object> to ArrayList<String>
ArrayList<Object> arrayList=new ArrayList<String>();//编译错误 Type mismatch: cannot convert from ArrayList<String> to ArrayList<Object>
ArrayList<Object> arrayList = new ArrayList<Object>();
ArrayList<String> arrayList2 = arrayList;//编译错误 Type mismatch: cannot convert from ArrayList<Object> to ArrayList<String>
ArrayList<String> arrayList = new ArrayList<String>();
ArrayList<Object> arrayList2 = arrayList;//编译错误 Type mismatch: cannot convert from ArrayList<String> to ArrayList<Object>
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
class Pair<T> {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
class DateInter extends Pair<Date> {
@Override
public void setValue(Date value) {
super.setValue(value);
}
@Override
public Date getValue() {
return super.getValue();
}
}
class Pair {
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
DateInter dateInter = new DateInter();
dateInter.setValue(new Date());//编译正确
dateInter.setValue(new Object());//编译错误 The method setValue(Date) in the type DateInter is not applicable for the arguments (Object)
ArrayList<String> arrayList = new ArrayList<String>();
System.out.println(arrayList instanceof ArrayList<?>);//true
System.out.println(arrayList instanceof ArrayList);//true
//System.out.println(arrayList instanceof ArrayList<Object>);//编译错误
//System.out.println(arrayList instanceof ArrayList<String>);//编译错误
Cannot perform instanceof check against parameterized type ArrayList<Object/String>.
无法对参数化类型ArrayList <Object/String>执行instanceof检查。
Use the form ArrayList<?> instead since further generic type information will be erased at runtime
使用形式ArrayList <?>,因为进一步的泛型类型信息将在运行时被擦除
public class Problem<T> extends Exception{...}
try{
}catch(Problem<Integer> e1){
}catch(Problem<Number> e2){
}
try{
}catch(Problem<Object> e1){
}catch(Problem<Object> e2){
}
public static <T extends Throwable> void doWork(Class<T> t){
try{
}catch(T e){ //编译错误
}
}
public static <T extends Throwable> void doWork(Class<T> t){
try{
}catch(T e){ //编译错误
}catch(IndexOutOfBounds e){
}
}
public static<T extends Throwable> void doWork(T t) throws T{
try{
}catch(Throwable realCause){
t.initCause(realCause);
throw t;
}
}
Pair<String>[] table = new Pair<String>[10]; //Cannot create a generic array of Pair<String>
T t= new T(); //ERROR Cannot instantiate the type T
public boolean equals(T value) {
//Name clash: The method equals(T) of type Pair<T> has the same erasure as equals(Object) of type Object but does not override it
//名称冲突:Pair <T>类型的方法equals(T)具有与Object类似的equals(Object)相同的擦除,但不覆盖它
return false;
}
class A implements Comparable<Integer> {
@Override
public int compareTo(Integer o) {
return 0;
}
}
class B extends A implements Comparable<String> {
//The interface Comparable cannot be implemented more than once with different arguments: Comparable<Integer> and Comparable<String>
//接口Comparable不能使用不同的参数多次实现:可比较的<Calendar>和Comparable <GregorianCalendar>
}
class A implements Comparable {//Comparable is a raw type. References to generic type Comparable<T> should be parameterized
@Override
public int compareTo(Object o) {
return 0;
}
}
class B extends A implements Comparable {
}
public class Test<T> {
public static T one; //编译错误 Cannot make a static reference to the non-static type T
public static T show(T one){ //编译错误 Cannot make a static reference to the non-static type T
return null;
}
}
public class Test<T> {
public static <T >T show(T one){//这是正确的
return null;
}
}
标签:cab compareto mis run 参考 效果 引用传递 避免 instead
原文地址:http://www.cnblogs.com/baiqiantao/p/7475477.html