java中的泛型的一些常见例子_第1页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、java中的泛型的一些常见例子public vo name(t name) = name; public v getage() return age; public void setage(v age) this.age = age; private t name; private v age; public ic void main(sing args) hello string, integer he = new hello string, integer he.setage(10); he.setname("rollen holt"); syste

2、m.out.print(he.getname() + " " + he.getage(); * author rollen-holt 泛型类的构造办法定义 class hello t, v hello(t name, v age) this.age = age; = name; public t getname() return name; public v getage() return age; private t name; private v age; public static void main(string args) hello stri

3、ng,integer he=new hello string,integer ("rollen",12); system.out.println(he.getname()+" "+he.getage(); * author rollen-holt 用法通配符 class info t info(t name) = name; private t name; class hello public static void fution(info ? temp) system.out.println("内容: "+tem

4、p); public static void main(string args) info string demo=new info string ("rollen"); function(demo); * author rollen-holt 泛型上限 class info t info(t age) this.age=age; private t age; class hello public static void function(info ? tends number temp) system.out.println("内容"+ temp);

5、public static void main(string args) info integer demo=new info integer function(demo); * author rollen-holt 泛型下限 class info t info(t age) this.age=age; private t age; class hello public static void function(info ? per string temp) system.out.println("内容"+ temp); public static void main(st

6、ring args) / 此处只能用法string 或者object info string demo=new info string ("rollen"); function(demo); * author rollen-holt 泛型和子类继承的限制 class info t class hello public static void main(string args) info string demo1=new info string info object demo2=new info object /demo2=demo1; 此处错误 * 上面的例子解释,一个类

7、的子类可以通过多态性被其父类实例化 * 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。 假如允许上面的条语句的话,会浮现: exception in thread "main" java.lang.error: unresolv compilation problem: type mismatch: cannot convert from info string to info object at hello.main(hello.java:12) 泛型接口的两种实现: * author rollen-holt 泛型接口的实现1 interface in

8、fo t public void say(); / 挺直在子类之后声明泛型 class hello t implements info t public static void main(string args) info string demo = new hello string demo.say(); public void say() system.out.println("hello"); * author rollen-holt 泛型接口的实现2 interface info t public void say(); / 在子类实现的接口中明确给出泛型类型 cl

9、ass hello implements info string public static void main(string args) info string demo = new hello(); demo.say(); public void say() system.out.println("hello"); * author rollen-holt 用法泛型通一传入参数的类型 class info t private t var; public t getvar() return var; public void setvar(t var) this.var =

10、 var; public string tostring() return this.var.tostring(); class hello public static void main(string args) info string demo1=new info string info string demo2=new info string demo1.setvar("rollen"); demo2.setvar("holt"); printa(demo1, demo2); / 此处传递的都是同一个string类型的 public static

11、t void printadd(info t demo1, info t demo2) system.out.println(demo1.getvar()+" "+demo2.getvar(); 否则的话如下所示:浮现错误: * author rollen-holt 用法泛型通一传入参数的类型 class info t private t var; public t getvar() return var; public void setvar(t var) this.var = var; public string tostring() return this.var.t

12、ostring(); class hello public static void main(string args) info string demo1=new info string info integer demo2=new info integer demo1.setvar("rollen"); demo2.setvar(30); /此处调用错误 printadd(demo1, demo2); / 此处传递的都是同一个string类型的 public static t void printadd(info t demo1, info t demo2) system.out.println(demo1.getvar()+" "+demo2.getvar(); exception in thread "main" java.lang.error

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论