美媒列新赛季前5球员:约基奇超字母哥升至第一,MVP得主排第六
北京时间7月30日,美媒评选了NBA新赛季前15球员,其中掘金当家球星约基
看下面这段代码:
private static int add(int a, int b) { System.out.println(a + "+" + b + "=" + (a + b)); return a + b;}private static float add(float a, float b) { System.out.println(a + "+" + b + "=" + (a + b)); return a + b;}private static double add(double a, double b) { System.out.println(a + "+" + b + "=" + (a + b)); return a + b;}如果没有泛型,要实现不同类型的加法,每种类型都需要重载一个add方法;通过泛型,我们可以复用为一个方法:
【资料图】
private static double add(T a, T b) { System.out.println(a + "+" + b + "=" + (a.doubleValue() + b.doubleValue())); return a.doubleValue() + b.doubleValue();} 泛型中的类型在使用时指定,不需要强制类型转换(类型安全,编译器会检查类型)
下面这段代码:
List list = new ArrayList();list.add("xxString");list.add(100d);list.add(new Person());我们在使用上述list中,list中的元素都是Object类型(无法约束其中的类型),所以在取出集合元素时需要人为的强制类型转化到具体的目标类型,且很容易出java.lang.ClassCastException`异常。
引入泛型,它将提供类型的约束,提供编译前的检查:
List list = new ArrayList();// list中只能放String, 不能放其它类型的元素 泛型的好处1、编译时,检查添加元素的类型,提高了安全性
2、减少了类型转换的次数,提高效率
3、不再提示编译警告
介绍1.泛型又称参数化类型,是Jdk5.0出现的新特性,解决数据类型的安全性问题
2.在类声明或实例化时只要指定好需要的具体的类型即可。
3.Java泛型可以保证如果程序在编译时没有发出警告,运行时就不会产生ClassCastException.异常。同时,代码更加简洁、健壮
4.泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值的类型,或者是参数类型。
代码举例:
public class Generic03 { public static void main(String[] args) { //注意,特别强调:E具体的数据类型在定义Person对象的时候指定,即在编译期间,就确定E是什么类型 Person person = new Person<>("dfdfs"); person.show();//String /** * Person类相当于下面这样子 * class Person{ * String s;//E表示s的数据类型,该数据类型在定义Person对象的时候指定,即在编译期间,就确定E是什么类型 * * public Person(String s) {//E也可以是参数类型 * this.s = s; * } * * public String f(){//返回类型使用E * return s; * } * public void show(){ * System.out.println(s.getClass());//显示s的运行类型 * } * } */ Person person2 = new Person<>(100); person2.show(); /** * Person类相当于下面这样子 * class Person{ * Integer s;//E表示s的数据类型,该数据类型在定义Person对象的时候指定,即在编译期间,就确定E是什么类型 * * public Person(Integer s) {//E也可以是参数类型 * this.s = s; * } * * public Integer f(){//返回类型使用E * return s; * } * public void show(){ * System.out.println(s.getClass());//显示s的运行类型 * } * } */ }}//泛型的作用是:可以在类声明时通过一个标识表示类中某个属性的类型,或者是某个方法的返回值的类型,或者是参数类型class Person{ E s;//E表示s的数据类型,该数据类型在定义Person对象的时候指定,即在编译期间,就确定E是什么类型 public Person(E s) {//E也可以是参数类型 this.s = s; } public E f(){//返回类型使用E return s; } public void show(){ System.out.println(s.getClass());//显示s的运行类型 }} 语法泛型的声明interface 接口名
说明:
1)其中,T,K,V不代表值,而是表示类型
2)任意字母都可以。常用T表示,是Type的缩写
泛型的实例化要在类名后面指定类型参数的值(类型)。如:
List strList = new ArrayList();Iterator iterator = customers.iterator(); 泛型使用举例需求:
1.创建3个学生对象
2.放入到HashSet中学生对象,使用.
3.放入到HashMap中,要求Key 是String name,Value就是学生对象
4.使用两种方式遍历
public class GenericExercise { public static void main(String[] args) { //使用泛型方式给HashSet放入3个学生对象 HashSet students = new HashSet<>(); students.add(new Student("jack",18)); students.add(new Student("tom",18)); students.add(new Student("ml",18)); //第一种遍历方式,增强for循环 for (Student student:students){ System.out.println(student); } //使用泛型方式给HashMap放入3个学生对象 HashMap hm = new HashMap<>(); hm.put("milan",new Student("milan",34)); hm.put("jack",new Student("jack",31)); hm.put("tom",new Student("tom",30)); //2.迭代器 Set> entries = hm.entrySet(); Iterator> iterator = entries.iterator(); System.out.println("=========================="); while (iterator.hasNext()){ Map.Entry next = iterator.next(); System.out.println(next.getKey()+"-"+next.getValue()); } }}class Student{ private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name="" + name + "\"" + ", age=" + age + "}"; }} 泛型使用注意事项1.interface 接口名
List list = new ArrayList();//这样写正确List list = new ArrayList();//这样写错误 2.在给泛型指定具体类型后,可以传入该类型或者其子类类型
3.泛型使用形式
//1.第一种方式List list1 = new ArrayList();//2.第二种方式List Iist2 = new ArrayList<>(); 3.如果我们这样写List list3=new ArrayList();默认给它的泛型是 E就是Object.,等价于ArrayList
关键词:
北京时间7月30日,美媒评选了NBA新赛季前15球员,其中掘金当家球星约基
兴业研究公司储能行业2023年7月报认为,2023年上半年,中国新型储能中
根据Apple的惯例,iOS17 0beta内测版很可能会在WWDC23开发者大会结束后
抱歉,您暂无查看德邦证券研报的权限,德邦证券研报仅对其白名单客户开
新华社北京7月29日电国家防总于29日12时将针对京津冀晋鲁豫6省份的防汛
记者|覃思悦7月29日晚,2023年福冈游泳世锦赛继续在福冈海洋会展中心
最近很多人询问机械胶囊该怎么用呢这个问题,今天来为大家解答一下,希
HELLO,我是智能手机网小溪,我来为大家解答以上问题。豪尔赫洛佩兹,
小常来为大家解答以上问题。车损险怎么报合适,车损险怎么算很多人还不
【白酒糟饲料业务有多香?路德环境拟募资4 39亿元新增30万吨产能】据路
腾讯智能汽车云升级“双专区”。
纵观全球文明史,无论是古埃及、美索不达米亚,还是中国古代,那些被称
新华社01:327月26日下午,习近平总书记在四川省德阳市考察三星堆博物馆
直播吧7月29日讯据德国转会市场网统计,米兰今夏引援花费9000万欧元,
佘天昆公路是连接佘山、天马山和小昆山的公路。在很多松江人的记忆中,
Copyright 2015-2022 中公双创网 版权所有 备案号:沪ICP备2022005074号-18 联系邮箱:5855973@qq.com
前8个月江苏进出口总值3.63万亿元,同比增长10.6%
据南京海关统计,今年前8个月,江苏省进出口总值3 63万亿元,同比(下同)增长10 6%,占全国进出口总值的13 3%,较全国整体增速高0 5个百分点
致敬老将!热爱可抵岁月漫长
<script src="http: tv people com cn img player v js">< script><script>showPlayer({id:" pvservice xml 202
微视频|字里藏情迎元宵
<script src="http: tv people com cn img player v js">< script><script>showPlayer({id:" pvservice xml 202