Java Tutorial/Class Definition/toString — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:00, 1 июня 2010
Содержание
Jakarta Commons toString Builder
import org.apache.rumons.lang.builder.ToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class Main {
private String id;
private String firstName;
private String lastName;
public Main() {
}
public String toString() {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE).append("id", id).append(
"firstName", firstName).append("lastName", lastName).toString();
}
public static void main(String[] args) {
Main example = new Main();
example.id = "1";
example.firstName = "First Name";
example.lastName = "Last Name";
System.out.println("example = " + example);
}
}
override the toString method in your classes
public class MainClass {
public static void main(String[] args) {
Employee emp = new Employee("Martinez", "Anthony");
System.out.println(emp.toString());
}
}
class Employee {
private String lastName;
private String firstName;
public Employee(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String toString() {
return "Employee[" + this.firstName + " " + this.lastName + "]";
}
}
Override toString() for Box class.
class Box {
double width;
double height;
double depth;
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
public String toString() {
return "Dimensions are " + width + " by " + depth + " by " + height + ".";
}
}
class toStringDemo {
public static void main(String args[]) {
Box b = new Box(10, 12, 14);
String s = "Box b: " + b; // concatenate Box object
System.out.println(b); // convert Box to string
System.out.println(s);
}
}
Reflection based toString() utilities
import java.lang.reflect.Field;
public class Main {
String hello = "world";
int i = 42;
public static void main(String args[]) {
System.out.println(Util.toString(new MyClass()));
System.out.println(Util.toString(new MyAnotherClass()));
}
}
class Util {
public static String toString(Object o) {
StringBuilder sb = new StringBuilder();
toString(o, o.getClass(), sb);
return o.getClass().getName()+ "\n"+sb.toString();
}
private static void toString(Object o, Class clazz, StringBuilder sb) {
Field f[] = clazz.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
f[i].setAccessible(true);
try {
sb.append(f[i].getName() + "=" + f[i].get(o)+"\n");
} catch (Exception e) {
e.printStackTrace();
}
}
if (clazz.getSuperclass() != null)
toString(o, clazz.getSuperclass(), sb);
}
}
class MyClass {
int i = 1;
private double d = 3.14;
}
class MyAnotherClass extends MyClass{
int f = 9;
}
Use a generic toString()
import java.lang.reflect.Field;
public class Main {
public static void main(String args[]) {
System.out.println(new MyClass().toString());
}
}
class MyClass {
String hello = "hi";
int i = 0;
public String toString() {
StringBuilder sb = new StringBuilder();
Class cls = getClass();
Field[] f = cls.getDeclaredFields();
for (int i = 0; i < f.length; i++) {
f[i].setAccessible(true);
try {
sb.append(f[i].getName()+"="+ f[i].get(this)+"\n");
} catch (Exception e) {
e.printStackTrace();
}
}
if (cls.getSuperclass().getSuperclass() != null) {
sb.append("super:"+ super.toString()+"\n");
}
return cls.getName()+"\n" + sb.toString();
}
}
Use Reflection To build toString method
import org.apache.rumons.lang.builder.ReflectionToStringBuilder;
import org.apache.rumons.lang.builder.ToStringStyle;
public class Main {
private Integer id;
private String name;
private String description;
public static final String KEY = "APP-KEY";
private transient String secretKey;
public Main(Integer id, String name, String description, String secretKey) {
this.id = id;
this.name = name;
this.description = description;
this.secretKey = secretKey;
}
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE, true, true);
}
public static void main(String[] args) {
Main demo = new Main (1, "A", "B", "C");
System.out.println("Demo = " + demo);
}
}