//Studnet package yzhou.gen03; public class Student<T> { private T score; public T getScore() { return score; } public void setScore(T score) { this.score = score; } }
//BjStu package yzhou.gen03; public class Student<T> { private T score; public T getScore() { return score; } public void setScore(T score) { this.score = score; } }
package yzhou.gen03; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.Map.Entry; /** * 泛型嵌套 -->由外到内拆分 * @author 洋 * */ public class StudentApp { public static void main(String[] args) { Student<String> stu = new Student<String>(); stu.setScore("优秀"); System.out.println(stu.getScore()); BjStu<Student<String>> bjstu = new BjStu<Student<String>>(); bjstu.setStu(stu); stu = bjstu.getStu(); String score = stu.getScore(); System.out.println(score); //HashMap ---> 泛型嵌套 Map<String,String> map = new HashMap<String,String>(); map.put("a", "java300"); map.put("b","zhangsan"); Set<Entry<String,String>> entrySet = map.entrySet(); //循环遍历 Map for(Entry<String, String> entry:entrySet) { String key = entry.getKey(); String value= entry.getValue(); System.out.println(key+"-->"+value); } } }
时间: 2024-10-14 23:13:17