🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
![](https://box.kancloud.cn/bca9b81e7c375612e8e8560b96ce1434_682x394.png) 泛型 ~~~ package arraylist.a5; import java.util.ArrayList; public class Test { public static void main(String[] args) { ArrayList<Ls> ff=new ArrayList<Ls>();//通过泛型将类型定下来,不用强转了 Ls ls1=new Ls("薯片",5f); ff.add(ls1); //Ls cls=(Ls)ff.get(0);//不用强转了 Ls cls=ff.get(0); System.out.println(cls.getMingcheng()); } } //零食 class Ls{ private String mingcheng; private float jiage; Ls(String mingcheng, float jiage) { super(); this.mingcheng = mingcheng; this.jiage = jiage; } public String getMingcheng() { return mingcheng; } public void setMingcheng(String mingcheng) { this.mingcheng = mingcheng; } } //颜料 class Yl{ private String mingcheng; private float jiage; private String yanse; Yl(String mingcheng, float jiage, String yanse) { super(); this.mingcheng = mingcheng; this.jiage = jiage; this.yanse = yanse; } } ~~~ ![](https://box.kancloud.cn/a24ae5d954c39243822770d9dea61e85_97x80.png) 反射机制 ~~~ package arraylist.a6; import java.lang.reflect.Method; public class Test { public static void main(String[] args) { Bh<String> hw1=new Bh<String>("货物1"); hw1.lxmc(); } } //玩具 class Wj{ public void wan(){ System.out.println("正在玩游戏"); } public int jiafa(int a,int b){ return a+b; } } //百货 class Bh<L>{ private L l; Bh(L l){ this.l=l; } public void lxmc(){ System.out.println("类型是:"+l.getClass().getName());//取类型名称 /*Method []a=l.getClass().getDeclaredMethods(); for(int i=0;i<a.length;i++){ System.out.println("函数名:"+a[i].getName()); }*/ } } ~~~ ![](https://box.kancloud.cn/2c5929a03b55108da14462708efc149d_309x66.png) ~~~ package arraylist.a6; import java.lang.reflect.Method; public class Test { public static void main(String[] args) { //Bh<String> hw1=new Bh<String>("货物1"); Bh<Integer> hw2=new Bh<Integer>(123); Bh<Wj> hw3=new Bh<Wj>(new Wj()); //hw1.lxmc(); //hw2.lxmc(); hw3.lxmc(); } } //玩具 class Wj{ public void wan(){ System.out.println("正在玩游戏"); } public int jiafa(int a,int b){ return a+b; } } //百货 class Bh<L>{ private L l; Bh(L l){ this.l=l; } public void lxmc(){ System.out.println("类型是:"+l.getClass().getName());//取类型名称 Method []a=l.getClass().getDeclaredMethods();//用数组来接收类中方法的数量 for(int i=0;i<a.length;i++){ System.out.println("函数名:"+a[i].getName()); } } } ~~~ ![](https://box.kancloud.cn/97b817d0634ec2915af30670ce5ef07e_292x121.png)