Java中的功能是否类似于C#的匿名类型? [英] Is there a functionality in Java similar to C#'s anonymous types?
本文介绍了Java中的功能是否类似于C#的匿名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道Java中是否存在类似于C#的匿名类型的类似功能:
I was wondering if there exists a similar functionality in Java similar to C#'s anonymous types:
var a = new {Count = 5, Message =A string。};
或者这个概念是否违反Java范式?
Or does this concept go against the Java paradigm?
编辑:
我认为使用 Hashable()
Java有点类似。
I suppose using Hashable()
in Java is somewhat similar.
推荐答案
也许你的意思是这样的:
Maybe you mean sth like this:
Object o = new Object(){
int count = 5;
String message = "A string.";
};
@Commenters :当然这是一个理论上非常不方便的例子。
@Commenters: of course this is a theoretical, very inconvenient example.
可能OP可能会使用地图
:
Map<String,Object> a = new HashMap<String,Object>();
a.put("Count", 5);
a.put("Message", "A string.");
int count = (Integer)a.get("Count"); //better use Integer instead of int to avoid NPE
String message = (String)a.get("Message");
这篇关于Java中的功能是否类似于C#的匿名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文