博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中的泛型的一些常见例子
阅读量:4220 次
发布时间:2019-05-26

本文共 5254 字,大约阅读时间需要 17 分钟。

/**
002     * @author Rollen-Holt 使用泛型
003     */
004    
005    class hello<T, V> {
006        hello(){
007    
008        }
009    
010        public T getName(){
011            return name;
012        }
013    
014        public void setName(T name){
015            this.name = name;
016        }
017    
018        public V getAge(){
019            return age;
020        }
021    
022        public void setAge(V age){
023            this.age = age;
024        }
025    
026        private T name;
027        private V age;
028    
029        public static void main(String[] args){
030            hello<String, Integer> he = new hello<String, Integer>();
031            he.setAge(10);
032            he.setName("Rollen Holt");
033            System.out.println(he.getName() + "   " + he.getAge());
034        }
035    }
036    
037    /**
038     * @author Rollen-Holt 泛型类的构造方法定义
039     */
040    
041    class hello<T, V> {
042    
043        hello(T name, V age){
044            this.age = age;
045            this.name = name;
046        }
047    
048        public T getName(){
049            return name;
050        }
051    
052        public V getAge(){
053            return age;
054        }
055    
056        private T name;
057        private V age;
058    
059        public static void main(String[] args){
060            hello<String,Integer> he=new hello<String,Integer>("Rollen",12);
061            System.out.println(he.getName()+"  "+he.getAge());
062        }
063    }
064    
065    /**
066     * @author Rollen-Holt 使用通配符
067     */
068    
069    class info<T> {
070        info(T name){
071            this.name = name;
072        }
073        private T name;
074    }
075    
076    class hello{
077        public static void function(info<?> temp){
078            System.out.println("内容: "+temp);
079        }
080         
081        public static void main(String[] args){
082            info<String> demo=new info<String>("Rollen");
083            function(demo);
084        }
085    }
086    
087    /**
088     * @author Rollen-Holt 泛型上限
089     */
090    
091    class info<T>{
092        info(T age){
093            this.age=age;
094        }
095        private T age;
096    }
097    
098    class hello{
099        public static void function(info<? extends Number> temp){
100            System.out.println("内容"+ temp);
101        }
102         
103        public static void main(String[] args){
104         
105          info<Integer> demo=new info<Integer>(1);
106          function(demo);
107        }
108    }
109    
110    /**
111     * @author Rollen-Holt 泛型下限
112     */
113    
114    class info<T>{
115        info(T age){
116            this.age=age;
117        }
118        private T age;
119    }
120    
121    class hello{
122        public static void function(info<? super String> temp){
123            System.out.println("内容"+ temp);
124        }
125         
126        public static void main(String[] args){
127         
128         // 此处只能使用String 或者Object
129          info<String> demo=new info<String>("Rollen");
130          function(demo);
131        }
132    }
133    /**
134     * @author Rollen-Holt 泛型和子类继承的限制
135     */
136    
137    class info<T>{
138    }
139    
140    class hello{
141        public static void main(String[] args){
142        info<String> demo1=new info<String>();
143        info<Object> demo2=new info<Object>();
144        //demo2=demo1;   此处错误
145          
146        }
147    }
148    
149    /**
150     * 上面的例子说明,一个类的子类可以通过多态性被其父类实例化
151     * 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。
152     */
153    如果允许上面的条语句的话,会出现:
154    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
155        Type mismatch: cannot convert from info<String> to info<Object>
156    
157        at hello.main(hello.java:12)
158    泛型接口的两种实现:
159    /**
160     * @author Rollen-Holt 泛型接口的实现1
161     */
162    
163    interface info<T> {
164        public void say();
165    }
166    
167    // 直接在子类之后声明泛型
168    class hello<T> implements info<T>{
169    
170        public static void main(String[] args){
171            info<String> demo = new hello<String>();
172            demo.say();
173    
174        }
175    
176        public void say(){
177            System.out.println("hello");
178    
179        }
180    }
181    
182    /**
183     * @author Rollen-Holt 泛型接口的实现2
184     */
185    
186    interface info<T> {
187        public void say();
188    }
189    
190    // 在子类实现的接口中明确给出泛型类型
191    class hello implements info<String>{
192    
193        public static void main(String[] args){
194            info<String> demo = new hello();
195            demo.say();
196    
197        }
198    
199        public void say(){
200            System.out.println("hello");
201    
202        }
203    }
204    /**
205     * @author Rollen-Holt 使用泛型通一传入参数的类型
206     */
207    
208    class info<T> {
209        private T var;
210    
211        public T getVar(){
212            return var;
213        }
214    
215        public void setVar(T var){
216            this.var = var;
217        }
218        public String toString(){
219            return this.var.toString();
220        }
221         
222    }
223    class hello{
224        public static void main(String[] args){
225            info<String> demo1=new info<String>();
226            info<String> demo2=new info<String>();
227            demo1.setVar("Rollen");
228            demo2.setVar("Holt");
229            printAdd(demo1, demo2);
230        }
231        // 此处传递的都是同一个String类型的
232        public static <T> void printAdd(info<T> demo1, info<T> demo2){
233            System.out.println(demo1.getVar()+" "+demo2.getVar());
234        }
235    }
236    否则的话如下所示:出现错误:
237    /**
238     * @author Rollen-Holt 使用泛型通一传入参数的类型
239     */
240    
241    class info<T> {
242        private T var;
243    
244        public T getVar(){
245            return var;
246        }
247    
248        public void setVar(T var){
249            this.var = var;
250        }
251        public String toString(){
252            return this.var.toString();
253        }
254         
255    }
256    class hello{
257        public static void main(String[] args){
258            info<String> demo1=new info<String>();
259            info<Integer> demo2=new info<Integer>();
260            demo1.setVar("Rollen");
261            demo2.setVar(30);
262            //此处调用错误
263            printAdd(demo1, demo2);
264        }
265        // 此处传递的都是同一个String类型的
266        public static <T> void printAdd(info<T> demo1, info<T> demo2){
267            System.out.println(demo1.getVar()+" "+demo2.getVar());
268        }
269    }
270    
271    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
272        The method printAdd(info<T>, info<T>) in the type hello is not applicable for the arguments (info<String>, info<Integer>)
273    
274        at hello.main(hello.java:26)

转载地址:http://dnlmi.baihongyu.com/

你可能感兴趣的文章
简单了解mysql表分区
查看>>
深入解析MySQL分区(Partition)功能
查看>>
mysql定时任务简单例子
查看>>
JDK8绿色安装详细步骤
查看>>
MySql最简单的触发器例子
查看>>
全选反选按钮简单例子
查看>>
Solr高效率索引查询简介
查看>>
jquery实现在页面上实现tr排序并后台处理详解
查看>>
浅谈jsp、freemarker、velocity区别
查看>>
第三方QQ,Sina登录平台 JS sdk
查看>>
js中的json对象和字符串之间的转化
查看>>
Java内存溢出的详细解决方案
查看>>
solr报Bad Request request: http://localhost:8080/solr/update?wt=javabin&version=2
查看>>
solr查询参数使用说明
查看>>
FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS
查看>>
模仿CSDN浏览器右下角弹出广告,兼容所有浏览器,内容可自定义,扩张性强
查看>>
对synchronized(this)的一些理解
查看>>
spring发送邮件时遇到错误org.springframework.mail.MailAuthenticationException: Authentication failed;
查看>>
jQuery 序列化表单数据 serialize() serializeArray()
查看>>
正则表达式语法
查看>>