手上有一个好网站怎么做赚钱,软件商城安装,小程序开发案例,有好看图片的软件网站模板泛型的基本原理
为什么需要泛型
在Java 5之前#xff0c;Java的集合类只能存储Object类型的对象。这意味着#xff0c;存储在集合中的对象在取出时需要进行类型转换#xff0c;这不仅繁琐#xff0c;而且容易出错。泛型通过在编译时进行类型检查#xff0c;确保类型安全…泛型的基本原理
为什么需要泛型
在Java 5之前Java的集合类只能存储Object类型的对象。这意味着存储在集合中的对象在取出时需要进行类型转换这不仅繁琐而且容易出错。泛型通过在编译时进行类型检查确保类型安全减少了运行时错误。
泛型的定义
泛型通过类型参数来实现这些类型参数在使用时被具体的类型替换。常见的类型参数命名如下
TType类型EElement元素KKey键VValue值
泛型类
泛型类是在类定义中使用类型参数。例如
class BoxT {private T content;public void setContent(T content) {this.content content;}public T getContent() {return content;}
}在使用泛型类时需要指定具体的类型
public class GenericClassExample {public static void main(String[] args) {BoxString stringBox new Box();stringBox.setContent(Hello, World!);System.out.println(stringBox.getContent()); // 输出: Hello, World!BoxInteger integerBox new Box();integerBox.setContent(123);System.out.println(integerBox.getContent()); // 输出: 123}
}泛型方法
泛型方法是在方法定义中使用类型参数。类型参数在方法名之前用尖括号指定。例如
public class GenericMethodExample {public static T void printArray(T[] array) {for (T element : array) {System.out.print(element );}System.out.println();}public static void main(String[] args) {Integer[] intArray {1, 2, 3, 4, 5};String[] stringArray {A, B, C, D, E};printArray(intArray); // 输出: 1 2 3 4 5 printArray(stringArray); // 输出: A B C D E }
}泛型和集合
Java集合框架是处理一组对象的标准方法。常见的集合包括List、Set和Map。结合泛型使用集合可以提高类型安全性和代码的可读性。
使用泛型集合
import java.util.ArrayList;
import java.util.List;public class GenericCollectionExample {public static void main(String[] args) {// 创建一个用于存储字符串的泛型列表ListString stringList new ArrayList();// 向列表中添加字符串stringList.add(Hello);stringList.add(World);// 使用增强型 for 循环遍历并打印列表中的每个字符串for (String str : stringList) {System.out.println(str);}}
}
//程序运行后输出结果为
//Hello
//World
在这个例子中ListString确保了stringList只能包含String类型的元素。
通配符
通配符用于在泛型中表示未知类型有三种主要形式
无界通配符?表示任何类型。有界通配符上界? extends T表示类型T及其子类型。有界通配符下界? super T表示类型T及其父类型。
无界通配符
无界通配符?表示任何类型。例如
import java.util.ArrayList;
import java.util.List;public class WildcardExample {public static void printList(List? list) {for (Object elem : list) {System.out.print(elem );}System.out.println();}public static void main(String[] args) {ListInteger intList new ArrayList();intList.add(1);intList.add(2);intList.add(3);ListString stringList new ArrayList();stringList.add(A);stringList.add(B);stringList.add(C);printList(intList); // 输出: 1 2 3 printList(stringList); // 输出: A B C }
}有界通配符上界
有界通配符? extends T表示类型T及其子类型。例如
import java.util.ArrayList;
import java.util.List;public class BoundedWildcardExample {public static double sumOfList(List? extends Number list) {double sum 0.0;for (Number number : list) {sum number.doubleValue();}return sum;}public static void main(String[] args) {ListInteger intList new ArrayList();intList.add(1);intList.add(2);intList.add(3);ListDouble doubleList new ArrayList();doubleList.add(1.1);doubleList.add(2.2);doubleList.add(3.3);System.out.println(Sum of intList: sumOfList(intList)); // 输出: Sum of intList: 6.0System.out.println(Sum of doubleList: sumOfList(doubleList)); // 输出: Sum of doubleList: 6.6}
}有界通配符下界
有界通配符? super T表示类型T及其父类型。例如
import java.util.ArrayList;
import java.util.List;public class LowerBoundedWildcardExample {public static void addNumbers(List? super Integer list) {list.add(1);list.add(2);list.add(3);}public static void main(String[] args) {ListNumber numberList new ArrayList();addNumbers(numberList);for (Number number : numberList) {System.out.println(number);}}
}泛型和继承
泛型不支持多态例如ListNumber不能被赋值为ListInteger。但是可以通过使用通配符来实现类似的效果。
示例泛型和继承
import java.util.ArrayList;
import java.util.List;class Person {private String name;public Person(String name) {this.name name;}public String getName() {return name;}
}class Employee extends Person {public Employee(String name) {super(name);}
}public class GenericInheritanceExample {public static void printNames(List? extends Person people) {for (Person person : people) {System.out.println(person.getName());}}public static void main(String[] args) {ListEmployee employees new ArrayList();employees.add(new Employee(Alice));employees.add(new Employee(Bob));printNames(employees); // 输出: Alice Bob}
}总结
通过以上更详细的解释和示例我们可以更深入地理解泛型和集合的使用
泛型类定义包含类型参数的类使用时指定具体类型。泛型方法定义包含类型参数的方法使用时指定具体类型。集合和泛型使用泛型集合可以保证类型安全。通配符表示未知类型提供更灵活的类型控制。泛型和继承使用通配符实现泛型的多态性。 文章转载自: http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.zrjzc.cn.gov.cn.zrjzc.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.kphsp.cn.gov.cn.kphsp.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.uqrphxm.cn.gov.cn.uqrphxm.cn http://www.morning.tgtrk.cn.gov.cn.tgtrk.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.xnpj.cn.gov.cn.xnpj.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.sgwr.cn.gov.cn.sgwr.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.qxmnf.cn.gov.cn.qxmnf.cn http://www.morning.cokcb.cn.gov.cn.cokcb.cn http://www.morning.bhwz.cn.gov.cn.bhwz.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.kncrc.cn.gov.cn.kncrc.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.ckwxs.cn.gov.cn.ckwxs.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.hwnnm.cn.gov.cn.hwnnm.cn http://www.morning.fslxc.cn.gov.cn.fslxc.cn http://www.morning.dgfpp.cn.gov.cn.dgfpp.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.qjngk.cn.gov.cn.qjngk.cn http://www.morning.tkxr.cn.gov.cn.tkxr.cn http://www.morning.jnvivi.com.gov.cn.jnvivi.com http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.rkck.cn.gov.cn.rkck.cn http://www.morning.qkzdc.cn.gov.cn.qkzdc.cn http://www.morning.qgcfb.cn.gov.cn.qgcfb.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.rbffj.cn.gov.cn.rbffj.cn http://www.morning.kxxld.cn.gov.cn.kxxld.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.zzbwjy.cn.gov.cn.zzbwjy.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.pjrql.cn.gov.cn.pjrql.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.mbzlg.cn.gov.cn.mbzlg.cn http://www.morning.njqpg.cn.gov.cn.njqpg.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.lqjpb.cn.gov.cn.lqjpb.cn http://www.morning.yrngx.cn.gov.cn.yrngx.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.lhxdq.cn.gov.cn.lhxdq.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.rqhn.cn.gov.cn.rqhn.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.pshpx.cn.gov.cn.pshpx.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn