您的位置:首页 > 职场人生

黑马程序员_基础加强之泛型

2014-06-17 23:47 381 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

  泛型是提供给javac编译器使用的,可以限定集合中的输入类型,让编译器挡住源程序中非法输入,编译器编译带类型说明的集合时会去除“类型”信息,使程序运行效率不受影响,对于参数化的泛型类型,getClass()方法的返回值和原始类型完全一样。由于编译生成的字节码会去掉泛型的类型信息,只要跳过编译器,就可以往某个泛型集合中加入其它类型的数据,例如,用反射得到集合,再调用其add方法。

  //定义一个GenericTest类

public class GenericTest {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
/*
*因为ArrayList不带类型参数,所以它可以添加任意类型的数据。
*/
ArrayList collection1 = new ArrayList();
collection1.add(1);
collection1.add(1L);
collection1.add("abc");
//通过get(index)获得的元素,其类型是Object,可以强转,获得真实类型
//int i = (Integer)collection1.get(1);

//加了类型参数
ArrayList<String> collection2 = new ArrayList<String>();
//元素的类型不能是String以外
//collection2.add(1);
//collection2.add(1L);
collection2.add("abc");
//取元素时,不需要强转,就可以得到元素的实际类型
String element = collection2.get(0);

//new String(new StringBuffer("abc"));
//通过反射方式,获得String(StringBuffer)的Constructor对象
Constructor<String> constructor1 = String.class.getConstructor(StringBuffer.class);
//相当于String str2 = new String(new StringBuffer("abc"))
//建立一个String对象
String str2 = constructor1.newInstance(/*"abc"*/new StringBuffer("abc"));
System.out.println(str2.charAt(2));

ArrayList<Integer> collection3 = new ArrayList<Integer>();
//getClass()返回集合的运行时类
System.out.println(collection3.getClass() == collection2.getClass());
//collection3.add("abc");
//调用add方法
collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abc");
System.out.println(collection3.get(0));

printCollection(collection3);

//Class<Number> x = String.class.asSubclass(Number.class);
//使用?通配符可以引用其他各种参数化的类型,?通配符定义的变量主要用作引用,可以调用与参数化无关的方法,不能调用与参数化有关的方法。
Class<?> y;
Class<String> x ;//Class.forName("java.lang.String");

HashMap<String,Integer> maps = new HashMap<String, Integer>();
maps.put("zxx", 28);
maps.put("lhm", 35);
maps.put("flx", 33);

Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
for(Map.Entry<String, Integer> entry : entrySet){
System.out.println(entry.getKey() + ":" + entry.getValue());
}

add(3,5);
Number x1 = add(3.5,3);
Object x2 = add(3,"abc");

GenericDao<ReflectPoint> dao = new GenericDao<ReflectPoint>();
dao.add(new ReflectPoint(3,3)); ;

ParameterizedType pType = (ParameterizedType)types[0];
System.out.println(pType.getRawType());
System.out.println(pType.getActualTypeArguments()[0]);
}

//一个通用的方法,它可以适应各种类型
private static <T> T add(T x,T y){
return null;
}

public static void printCollection(Collection<?> collection){
//collection.add(1);
System.out.println(collection.size());
for(Object obj : collection){
System.out.println(obj);
}
}

public static <T> void printCollection2(Collection<T> collection){
//collection.add(1);
System.out.println(collection.size());
for(Object obj : collection){
System.out.println(obj);
}

}

public static <T> void copy1(Collection<T> dest,T[] src){

}

public static <T> void copy2(T[] dest,T[] src){

}
}



  定义一个GenericDao类

//dao data access object--->crud
/*
*DAO调用数据库,数据库返回数据给DAO,并封装成VO(值对象,与表对应)
*/
public class GenericDao<E>  {//E是类型参数
public void add(E x){

}

public E findById(int id){
return null;
}

public void delete(E obj){

}

public void delete(int id){

}

public void update(E obj){

}

public static <E> void update2(E obj){

}

public E findByUserName(String name){
return null;
}
public Set<E> findByConditions(String where){
return null;
}
}
  

  泛型的好处

  java语言中引入泛型是一个较大的功能增强。这带来了很多好处:

  类型安全。 泛型的主要目标是提高Java程序的类型安全。通过知道使用泛型定义的变量的类型限制,编译器可以在一个高得多的程度上验证类型假设。没有泛型,这些假设就只存在于程序员头脑中(或者如果幸运的话,还存在于代码注释中)。

  java程序中的一种流行技术是定义这样的集合,即它的元素或键是公共类型的,比如“String 列表”或者“String 到 String 的映射”。通过在变量声明中捕获这一附加的类型信息,泛型允许编译器实施这些附加的类型约束。类型错误现在就可在编译时被捕获了。在编译时进行类型检查可以使我们更容易找到错误,并可提高程序的可靠性。

  消除强制类型转换,使代码可读性更强。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐