码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA泛型

时间:2019-01-17 20:07:52      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:system   new   strong   exce   pre   exception   obj   except   img   

当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常

 1 package cn.nn;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 
 6 public class Generic_test {
 7 
 8     public static void main(String[] args) {
 9         ArrayList arr = new ArrayList();
10         arr.add(new Tiger("华南虎"));
11         arr.add(new Tiger("东北虎"));
12         arr.add(new Sheep("喜羊羊"));
13 
14         System.out.println(arr);
15 
16         
17           Iterator it = arr.iterator(); 
18           while (it.hasNext()) {
19               Tiger nextObject = (Tiger)it.next();
20               Tiger tiger = nextObject; 
21               tiger.eat();
22           
23           }
24          
25     }
26 }
27 
28 class Tiger {
29     String name;
30 
31     public Tiger() {
32 
33     }
34 
35     public Tiger(String name) {
36         this.name = name;
37     }
38 
39     public String toString() {
40         return "Tiger@name:" + this.name;
41     }
42 
43     public void eat() {
44         System.out.println(this.name + "吃羊");
45     }
46 
47 }
48 
49 class Sheep /*extends Tiger*/{
50     String name;
51 
52     public Sheep() {
53 
54     }
55 
56     public Sheep(String name) {
57         this.name = name;
58     }
59 
60     public String toString() {
61         return "Sheep@name:" + this.name;
62     }
63     public void eat() {
64         System.out.println(this.name + "吃青草");
65         
66     }
67 }

出现 Exception in thread "main" java.lang.ClassCastException: cn.nn.Sheep cannot be cast to cn.nn.Tiger at cn.nn.Generic_test.main(Generic_test.java:19)错误。

 

技术分享图片

 1 package cn.nn;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 
 6 public class Generic_test {
 7 
 8     public static void main(String[] args) {
 9         ArrayList arr = new ArrayList();
10         arr.add(new Tiger("华南虎"));
11         arr.add(new Tiger("东北虎"));
12         arr.add(new Sheep("喜羊羊"));
13 
14         System.out.println(arr);
15 
16         
17           Iterator it = arr.iterator(); 
18           while (it.hasNext()) {
19               Tiger nextObject = (Tiger)it.next();
20               Tiger tiger = nextObject; 
21               tiger.eat();
22           
23           }
24          
25     }
26 }
27 
28 class Tiger {
29     String name;
30 
31     public Tiger() {
32 
33     }
34 
35     public Tiger(String name) {
36         this.name = name;
37     }
38 
39     public String toString() {
40         return "Tiger@name:" + this.name;
41     }
42 
43     public void eat() {
44         System.out.println(this.name + "吃羊");
45     }
46 
47 }
48 
49 class Sheep extends Tiger{
50     String name;
51 
52     public Sheep() {
53 
54     }
55 
56     public Sheep(String name) {
57         this.name = name;
58     }
59 
60     public String toString() {
61         return "Sheep@name:" + this.name;
62     }
63     public void eat() {
64         System.out.println(this.name + "吃青草");
65         
66     }
67 }

技术分享图片

 

JAVA泛型

标签:system   new   strong   exce   pre   exception   obj   except   img   

原文地址:https://www.cnblogs.com/LQLin168/p/10283531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!