java 创建匿名对象 类似于c# 中的 new { a:"aaa",b:"bbb"};

 

1 创建匿名对象
Object myobj = new Object() { public final boolean success = true; }
2 字面量方式初始化map

Map<String, String> myMap = new HashMap<>() {{

        put("c", "d");
    }};
}
3 初始化数组
var refEntityIDs = new ArrayList<String>(Arrays.asList(
     BalanceConst.BEIDVoucherOptFail,
BalanceConst.BEIDAccountingDocument,
BalanceConst.BEIDAccDocOptPreTbl
));
 

参考

https://stackoverflow.com/questions/8488833/what-is-the-java-equivalent-of-creating-an-anonymous-object-in-c

https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way

相关文章:

  • 2022-01-11
  • 2021-11-06
  • 2021-11-06
  • 2021-09-28
  • 2022-12-23
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
  • 2022-02-07
相关资源
相似解决方案