首页 技术 正文
技术 2022年11月12日
0 收藏 616 点赞 2,389 浏览 1371 个字

示例:

 public class Apple {
private static long counter;
private final long id = counter++; public long id(){
return id;
}
}
 public class Orange { }
 public class ApplesAndOrangesWithoutGenerics {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
//定义一个ArrayList容器, 不指定其类型
ArrayList apples = new ArrayList();
for (int i = 0; i < 3; i++) {
apples.add(new Apple());
} apples.add(new Orange()); //此时, apples容器中存在4个对象, 其中前三个为Apple类型, 最后一个为Orange类型
for (int i = 0; i < apples.size(); i++) {
//get()方法取值时, 得到的只是Object的引用, 必须将其强制转型为Apple, 否则编译错误
//当试图将Orange对象转型为Apple时, 发生类型转换异常
System.out.println(((Apple)apples.get(i)).id());
/*
* output:
* 0
* 1
* 2
* */
}
}
}

在本例中,因为ArrayList保存的是Object,所以可以将Apple对象和Orange对象放进容器中,当在使用ArrayList的get()方法来取出Apple对象时,得到的只是Object的引用,必须将其转型为Apple,因此,需在调用Apple的id()方法之前,强制进行转型,否则,

就会得到编译错误。

刚才声明容器时没有预先定义类型,默认为Object,现在使用预定义泛型来看看:

 public class ApplesAndOrangesWithoutGenerics2 {
public static void main(String[] args) {
//定义一个保存Apple对象的ArrayList, 尖括号括起来的是类型参数
//它指定了这个容器示例可以保存的类型, 通过使用泛型, 就可以在编译器放置将错误类型的对象放置到容器中
ArrayList<Apple> apples = new ArrayList<Apple>();
for (int i = 0; i < 3; i++) {
apples.add(new Apple());
} //apples.add(new Orange()); 编译器可以阻止将Orange放置到apples中 for (int i = 0; i < apples.size(); i++) {
System.out.println(apples.get(i).id());
} for (Apple c : apples) {
System.out.println(c.id());
} /*output
* 0
* 1
* 2
* 0
* 1
* 2
* */
}
}

我们注意到,定义了容器类型后,编译器可以阻止将Orange放置到apples中,因为此时Orange对象的类型与容器类型不匹配,发生编译错误;另外,将元素从容器中取出时,类型转换也不再时必须的了,因为容器知道自己保存的是什么类型,因此会在调用

get()时帮忙转型。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291