[java]代码库
/**
* 功能:泛型的简单说明
*/
package com.zjr;
import java.lang.reflect.Method;
public class Text1 {
public static void main(String args[]){
Gen<Bird> gen=new Gen<Bird>(new Bird());
gen.showTypeName();
}
}
//定义一个鸟类
class Bird{
public void text(){
System.out.println("Hello!");
}
public void sum(int a,int b){
System.out.println(a+b);
}
}
//定义一个类
class Gen<T>{
private T o;
//构造函数
public Gen(T a){
o=a;
}
//得到T的类型名称
public void showTypeName(){
System.out.println("类型:"+o.getClass().getName());
//通过反射机制,可以得到T类的许多信息,如函数的名称,返回类型等等
Method[] m=o.getClass().getDeclaredMethods();
//打印
for (int i=0;i<m.length;i++){
System.out.println(m[i].getName());
}
}
}
初级程序员
by: 云代码会员 发表于:2016-10-15 07:46:29 顶(0) | 踩(0) 回复
頂
回复评论