我一直在使用一个小的泛型方法来创建来自vararg元素的集合,例如
public <T> Set<T> createSet( T... elements ) { ...
Run Code Online (Sandbox Code Playgroud)
然而,最近我遇到了编译器没有按照我的预期行事的情况.以下createSet()仅使用s3作品.
Set<Class<? extends Throwable>> s1 = createSet( Exception.class, RuntimeException.class );
Set<? extends Class<Throwable>> s2 = createSet( Exception.class, RuntimeException.class );
Set<? extends Class<? extends Throwable>> s3 = createSet( Exception.class, RuntimeException.class );
Run Code Online (Sandbox Code Playgroud)
任何人都可以清楚地解释为什么s3可以工作以及我的思考可能出错的问题是s1 - 这是我的初始编码?谢谢.
是否有将Java对象序列化为Java代码的实现?例如,如果我有对象
Map<String,Integer> m = new Map<String,Integer>();
m.put("foo",new Integer(21));
Run Code Online (Sandbox Code Playgroud)
我可以使用它来序列化
ObjectOutputStream out = new ObjectOutputStream( ... );
out.writeObject( m );
out.flush();
Run Code Online (Sandbox Code Playgroud)
例如,输出就是
java.util.Map<String,Integer> m = new java.util.Map<String,Integer>();
m.put("foo",new Integer(21));
Run Code Online (Sandbox Code Playgroud)
你为什么要这个?有时,以编程方式部分创建复杂对象更容易,然后在代码中手动完成创建.然后可以将此代码包含在源代码中,并使用其他所有内容控制版本.请注意,使用外部序列化对象不是例外.
谢谢你提供的所有帮助.