推荐于2016-05-03 · 知道合伙人数码行家
关注
展开全部
在ArrayList容器类中使用toString()
编写一个工具类,用于在控制台输出Iterator。
import java.util.Iterator;
public class Printer {
static void printAll(Iterator e){
while(e.hasNext()){
System.out.println(e.next());
}
}
}
在Hamster类中重写父类的toString()方法。
public class Hamster {
private int hamsterNumber;
public Hamster(int hamsterNumber){
this.hamsterNumber=hamsterNumber;
}
public String toString(){
return "This is Hamster #"+hamsterNumber;
}
}
在HamsterMaze类中使用容器类加载Hamster类对象并输出结果。
import java.util.ArrayList;
import java.util.List;
public class HamsterMaze {
@SuppressWarnings("unchecked")
public static void main(String[] args){
List list=new ArrayList();
for(int i=0;i<3;i++)
list.add(new Hamster(i));
Printer.printAll(list.iterator());
}
}
结果:
This is Hamster #0
This is Hamster #1
This is Hamster #2
3.一个实现toString()的通用的Bean
在作一个项目时发现,许多bean需要实现toString()方法,就实现一个通用的bean,然后通过其他继承即可。
import java.lang.reflect.Field;
public class BaseBean {
public String toString() {
StringBuffer sb = new StringBuffer();
try {
Class t = this.getClass();
Field[] fields = t.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
field.setAccessible(true);
sb.append("{");
sb.append(field.getName());
sb.append(":");
if (field.getType() == Integer.class) {
sb.append(field.getInt(this));
} else if (field.getType() == Long.class) {
sb.append(field.getLong(this));
} else if (field.getType() == Boolean.class) {
sb.append(field.getBoolean(this));
} else if (field.getType() == char.class) {
sb.append(field.getChar(this));
} else if (field.getType() == Double.class) {
sb.append(field.getDouble(this));
} else if (field.getType() == Float.class) {
sb.append(field.getFloat(this));
} else
sb.append(field.get(this));
sb.append("}");
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
测试类
public class TestBean extends BaseBean {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static void main(String[] args) {
TestBean testBean = new TestBean();
testBean.setId(9);
System.out.println(testBean.toString());
}
}
结果
{id:9}
编写一个工具类,用于在控制台输出Iterator。
import java.util.Iterator;
public class Printer {
static void printAll(Iterator e){
while(e.hasNext()){
System.out.println(e.next());
}
}
}
在Hamster类中重写父类的toString()方法。
public class Hamster {
private int hamsterNumber;
public Hamster(int hamsterNumber){
this.hamsterNumber=hamsterNumber;
}
public String toString(){
return "This is Hamster #"+hamsterNumber;
}
}
在HamsterMaze类中使用容器类加载Hamster类对象并输出结果。
import java.util.ArrayList;
import java.util.List;
public class HamsterMaze {
@SuppressWarnings("unchecked")
public static void main(String[] args){
List list=new ArrayList();
for(int i=0;i<3;i++)
list.add(new Hamster(i));
Printer.printAll(list.iterator());
}
}
结果:
This is Hamster #0
This is Hamster #1
This is Hamster #2
3.一个实现toString()的通用的Bean
在作一个项目时发现,许多bean需要实现toString()方法,就实现一个通用的bean,然后通过其他继承即可。
import java.lang.reflect.Field;
public class BaseBean {
public String toString() {
StringBuffer sb = new StringBuffer();
try {
Class t = this.getClass();
Field[] fields = t.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
field.setAccessible(true);
sb.append("{");
sb.append(field.getName());
sb.append(":");
if (field.getType() == Integer.class) {
sb.append(field.getInt(this));
} else if (field.getType() == Long.class) {
sb.append(field.getLong(this));
} else if (field.getType() == Boolean.class) {
sb.append(field.getBoolean(this));
} else if (field.getType() == char.class) {
sb.append(field.getChar(this));
} else if (field.getType() == Double.class) {
sb.append(field.getDouble(this));
} else if (field.getType() == Float.class) {
sb.append(field.getFloat(this));
} else
sb.append(field.get(this));
sb.append("}");
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
}
测试类
public class TestBean extends BaseBean {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static void main(String[] args) {
TestBean testBean = new TestBean();
testBean.setId(9);
System.out.println(testBean.toString());
}
}
结果
{id:9}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询