题目01
泛型方法
package com.jerry.java; import java.util.*; /** * @author jerry_jy * @create 2022-10-07 15:51 */ public class Exer1 { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>();//类型推断 list.add(78); list.add(88); list.add(77); list.add(66); //遍历方式一: for (Integer integer : list) { //不需要强转 System.out.println(integer); } System.out.println(); //遍历方式二: Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } // HashMap<String, Integer> map = new HashMap<>(); // map.put("Tom1",34); // map.put("Tom2",44); // map.put("Tom3",33); // map.put("Tom4",32); // // Set<Map.Entry<String, Integer>> entrySet = map.entrySet(); // Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator(); // while (iterator.hasNext()){ // Map.Entry<String, Integer> entry = iterator.next(); // System.out.println(entry.getKey() + "--->" + entry.getValue()); // } } }
题目02
泛型集合
package com.jerry.java; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; /** * @author jerry_jy * @create 2022-10-07 18:17 */ public class Exer2 { public static void main(String[] args) { List<?> list = null; list = new ArrayList<String>(); list = new ArrayList<Double>(); // list.add(3);//编译不通过 list.add(null); List<String> l1 = new ArrayList<String>(); List<Integer> l2 = new ArrayList<Integer>(); l1.add("尚硅谷"); l2.add(15); read(l1); read(l2); } public static void read(List<?> list) { for (Object o : list) { System.out.println(o); } } public static void printCollection(Collection c) { Iterator i = c.iterator(); for (int k = 0; k < c.size(); k++) { System.out.println(i.next()); } } // public void printCollection(Collection<Object> c) { // for (Object e : c) { // System.out.println(e); // } // } public void testGenericAndSubClass() { Person[] persons = null; Man[] mans = null; // 而 Person[] 是 Man[] 的父类. persons = mans; Person p = mans[0]; // 在泛型的集合上 List<Person> personList = null; List<Man> manList = null; // personList = manList;//(报错) } }
题目03
泛型继承
package com.jerry.java; /** * @author jerry_jy * @create 2022-10-07 18:13 */ public class PersonTest { public static <T extends Person> void test(T t){ System.out.println(t); } public static void main(String[] args) { test(new Person()); test(new Man()); //test(new Creature());//reason: no instance(s) of type variable(s) exist so that Creature conforms to Person } } class Creature{} class Person extends Creature{} class Man extends Person{}
题目04
泛型测试
package com.jerry.java; import java.util.ArrayList; /** * @author jerry_jy * @create 2022-10-07 17:56 */ public class GenericTest { public static void main(String[] args) { // 1、使用时:类似于Object,不等同于Object ArrayList list = new ArrayList(); // list.add(new Date());//有风险 list.add("hello"); test(list);// 泛型擦除,编译不会类型检查 // ArrayList<Object> list2 = new ArrayList<Object>() // test(list2);//一旦指定Object,编译会类型检查,必须按照Object处理 } public static void test(ArrayList<String> list) { String str = ""; for (String s : list) { str += s + ","; } System.out.println("元素:" + str); } }
题目05
泛型嵌套
package com.jerry.java; /** * @author jerry_jy * @create 2022-10-07 18:58 */ public class GenericPerson { public static void main(String args[]) { Person1<Contact> per = null; // 声明Person对象 per = new Person1<Contact>(new Contact("北京市", "01088888888", "102206")); System.out.println(per); Person1<Introduction> per2 = null; // 声明Person对象 per2 = new Person1<Introduction>(new Introduction("李雷", "男", 24)); System.out.println(per2); } } interface Info { // 只有此接口的子类才是表示人的信息 } class Contact implements Info { // 表示联系方式 private String address; // 联系地址 private String telephone; // 联系方式 private String zipcode; // 邮政编码 public Contact(String address, String telephone, String zipcode) { this.address = address; this.telephone = telephone; this.zipcode = zipcode; } public void setAddress(String address) { this.address = address; } public void setTelephone(String telephone) { this.telephone = telephone; } public void setZipcode(String zipcode) { this.zipcode = zipcode; } public String getAddress() { return this.address; } public String getTelephone() { return this.telephone; } public String getZipcode() { return this.zipcode; } @Override public String toString() { return "Contact [address=" + address + ", telephone=" + telephone + ", zipcode=" + zipcode + "]"; } } class Introduction implements Info { private String name; // 姓名 private String sex; // 性别 private int age; // 年龄 public Introduction(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public String getSex() { return this.sex; } public int getAge() { return this.age; } @Override public String toString() { return "Introduction [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } } class Person1<T extends Info> { private T info; public Person1(T info) { // 通过构造器设置信息属性内容 this.info = info; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } @Override public String toString() { return "Person [info=" + info + "]"; } }
题目06
/* 开发一个泛型Apple类,要求有一个重量属性weight在测试类中实例化不同的泛型对象,要求对象a1的这一属性是String类型,对象a2的这一属性是Integer型,a3的这一属性是Double型。分别为a1,a2,a3的重量属性赋值为:”500克”,500,500.0,在测试类中通过对象调用访问器得到属性值并输出。另外思考,为什么a2和a3的属性需要是Integer和Double而不是int和double? */
package com.jerry.exer; /** * @author jerry_jy * @create 2022-10-07 19:05 */ public class Exer1 { public static void main(String[] args) { Apple<String> a1 = new Apple<>("500克"); Apple<Integer> a2 = new Apple<>(500); Apple<Double> a3 = new Apple<>(500.0); System.out.println(a1.toString()); System.out.println(a2.toString()); System.out.println(a3.toString()); } } class Apple<T>{ T weight; public Apple(T weight) { this.weight = weight; } public T getWeight() { return weight; } public void setWeight(T weight) { this.weight = weight; } @Override public String toString() { return "Apple{" + "weight=" + weight + '}'; } }
题目07
/* 封装一个新闻类News,包含新闻标题,新闻作者,新闻内容,新闻类型三个属性,提供必要的访问器和修改器方法,重写toString方法,要求打印对象时输出格式为“标题;类型;作者”,要求只要新闻标题相同就判断为同一条新闻。在测试类中创建一个只能容纳该类对象的ArrayList集合,添加三条新闻。遍历集合,打印新闻标题,将新闻标题截取字符串到10个汉字的长度。 */
package com.jerry.exer; import java.util.ArrayList; /** * @author jerry_jy * @create 2022-10-07 19:11 */ public class Exer2 { public static void main(String[] args) { News n1 = new News("更具传染性、逃逸性!国内一地检出新变异株", "北京日报客户端", "又有新的新冠奥密克戎变异株在我国本土出现。", "要闻"); News n2 = new News("这个十一假期,城市周边游有点贵", "界面新闻", "十一小长假结束,在倡导就地过节的背景下,今年国庆假期出行人数较去年大幅降低。", "国内"); News n3 = new News("国乒女团3比0完胜中国台北队 7连胜挺进决赛", "新华社", "7日,在成都进行的2022年第56届世界乒乓球团体锦标赛(决赛)女团半决赛中,中国队派出孙颖莎、陈梦、王曼昱迎战中国台北队,最终3:0完胜对手,率先闯进本次世乒赛决赛", "体育"); News n4 = new News("大规模研究称默沙东新冠口服药无法降低住院风险,小分子药遭质疑", "第一财经网", "服用默沙东口服药物莫诺拉韦(molnupiravir),虽然可以缩短患者的康复时间,但与安慰剂组相比,该药物在降低住院率方面没有任何益处。", "财经"); ArrayList<News> list = new ArrayList<>(); list.add(n1); list.add(n2); list.add(n3); list.add(n4); System.out.println(list); Object[] array = list.toArray(); for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } } class News{ private String title,author,context,newsType; public News(String title, String author, String context, String newsType) { this.title = title; this.author = author; this.context = context; this.newsType = newsType; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public String getNewsType() { return newsType; } public void setNewsType(String newsType) { this.newsType = newsType; } @Override public String toString() { return "News{" + "title='" + title + '\'' + ", author='" + author + '\'' + ", newsType='" + newsType + '\'' + '}'; } public boolean equals(Object o){ if (o instanceof News){ News news = (News) o; return this.getTitle().equals(news.getTitle()); } throw new RuntimeException("传入的数据类型不一致!"); } }