下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 初中九年级数学《二次函数与一元二次方程》教学设计(第1课时)
- 六年级综合实践活动《找个岗位去体验》教学设计
- 高中物理教学设计:自感与涡流现象的深度探究与核心素养培育
- 高三生物一轮复习《细胞膜与细胞核的结构和功能》教学设计
- 九年级地理《地球的宇宙环境与运动》专题复习教学设计
- 八年级劳动教育《花卉繁殖与养护》教学设计
- 高中政治选择性必修一 教学设计:中国与新兴国际组织的合作逻辑与实践路径
- 小学五年级德育教学设计:摆脱假努力学会真自律
- 初中九年级科学“项目化学习”专题教学设计-生物与化学融合视角下的校园水生态调查与水质改良方案
- 三年级综合实践活动《设计营养早餐》教学设计
- 市政路面白改黑改造工程监理细则
- 第15课 规划与设计教学设计-2025-2026学年小学信息技术(信息科技)五年级第5册滇人版
- DBJ50T-542-2026 建筑机器人应用技术标准
- 中考物理总复习《浮力与压强》专项测试卷及答案
- 产品质量责任考核制度
- 可控硅系列培训课件
- 2026届新疆石河子市石河子二中高一上数学期末检测模拟试题含解析
- 1.2 1.2.1 命题与量词 课件-2026版高中数学人教B版必修第一册
- 麻醉睡眠门诊科普
- 压力管道设计培训
- 农行笔试真题全套及答案
评论
0/150
提交评论