我觉得要简单呢,就用 Map 好了。如果你想传递过去的数据有明确类型,貌似只能自己定义类型或接口了。不过如果是用于转换成 JSON,用 Map 应该比较好。
另外,也可以用匿名类,
class User {
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
private String name;
public int getAge() {
return age;
}
private int age;
}
class Customer {
public Customer(String name, double money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
private String name;
public double getMoney() {
return money;
}
private double money;
}
public class Test extends Thread {
public static void main(String[] args) {
final User _user = new User("James", 30);
final Customer _customer = new Customer("Andy", 100.00);
// 匿名类实现组合类型
Object combine = new Object() {
{
this.user = _user;
this.customer = _customer;
}
private User user;
private Customer customer;
public User getUser() {
return user;
}
public Customer getCustomer() {
return customer;
}
public String toString() {
return user.getName() + "|" + customer.getName();
}
};
System.out.println(combine.toString());
}
}
如果需要明确的接口,可以自定义一个接口,将代码代码中 new Object() { .... } 中那个 Object 替换成你自己的接口——不过写这么麻烦,都可以自己定义个组合类型了。你可以自己研究下自定义 Tuple 泛型(网上貌似也能搜到)。