首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java基础-反照

2013-08-01 
java基础-反射我们每次要重载to_String的时候可以使用import java.lang.reflect.Fieldimport java.lang.r

java基础-反射
我们每次要重载to_String的时候可以使用

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DeamonThread {
private int i;
private int j;

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}

public void setJ(int j) {
this.j = j;
}

public String toString(){
StringBuffer buf = new StringBuffer();
Method[] me      = this.getClass().getMethods();
for(int i=0;i<me.length;i++){
if(!me[i].getName().equals("getClass")){
if(me[i].getName().substring(0, 3).equals("get")){
try {
buf.append("<"+me[i].invoke(this,null)+">");
} catch (Exception e) {
}
}
}
}
return buf.toString();
}

public static void main(String[] args) {
DeamonThread t = new DeamonThread();
t.setI(1222222);
t.setJ(111);
System.out.println("getxxx".substring(0, 3).equals("get"));
System.out.println(t.toString());
}
}

热点排行