我对 Goggles Gson 库有疑问。看下面的代码:
public abstract class Main {
public String foo = "foo";
public List<String> bar = Arrays.asList( "foo", "bar" );
@Override
public String toString( ) {
Gson gson = new Gson( );
return gson.toJson( this );
}
public static void main( String[] args ) {
Main main = new Main( ) {
};
System.out.println(main.toString( ));
}
}
它打印null
。但是我希望它打印{"foo":"foo","bar":["foo","bar"]}
它所做的,当我在创建Main( )
.
那么如何为抽象类获得正确的输出呢?