简体   繁体   English

Java 中的 C# object 变量的等价物是什么?

[英]What is the equivalent of a C# object variable in Java?

I have this C# code:我有这个 C# 代码:

var invoice = new {
       code = "01",
       otherThings = "etc",
       ...
  }

What would be the equivalent of this in Java? Java 中的等效项是什么?

I tried creating a: Object[] invoice = new Object[]{} but don't know how to fill the object.我尝试创建一个: Object[] invoice = new Object[]{}但不知道如何填写 object。

I hope I was clear enough, thanks.我希望我足够清楚,谢谢。

You can write it like that:你可以这样写:

Invoice i = new Invoice(){...};

But thats not Clean Code.但这不是干净的代码。

just write a constructor with all properties like:只需编写一个具有所有属性的构造函数,例如:

public invoice(String code, String otherThings)
{
     this.code = code;
     this.otherThings = otherThings;
}

... ...

Invoice i = new Invoice("asdfasdf", "asdfasdf");

Oject[] invoice = new Object[5];
invoice[0] = (Object)i;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM