public void getToStringSTR(){
Field[] fs = this.getClass().getDeclaredFields();
for (int i = 0; i < fs.length; i++) {
}
List<String> fsnames = new ArrayList<String>();
for(int i = 0 ; i <fs.length ; i ++ ){
if(!fs[i].getName().endsWith("serialVersionUID")){
fsnames.add(fs[i].getName());
}
}
StringBuffer sb = new StringBuffer();
//属性个数为0
if(fs.length==0){
sb.append("return this.getClass().getName() +\"【");
sb.append("】\";");
}
//属性个数大于0
if(fs.length>0){
sb.append("return this.getClass().getName() +\"【");
//属性个数为1
if(fsnames.size()==1){
sb.append(""+fsnames.get(0)+"=\"+"+fsnames.get(0)+"");
sb.append("+\"】\";");
}
//属性个数大于1
if(fsnames.size()>1){
for (int j = 0; j < fs.length; j++) {
if(j<fs.length-1){
sb.append(""+fsnames.get(j)+"=\"+"+fsnames.get(j)+"+\",");
}
if(j==fsnames.size()-1){
sb.append(""+fsnames.get(j)+"=\"+"+fsnames.get(j)+"");
sb.append("+\"】\";");
}
}
}
}
System.out.println("public String toString(){");
System.out.println(" "+sb);
System.out.println("}");
}
public static void main(String[] args) {
new User().getToStringSTR();
}
-
-