[英]Java initialize class in style like in C#
I'm quite base in Java because I'm full time C# developer but i need make some private project in Android. 我是Java的基础,因为我是全职的C#开发人员,但我需要在Android中制作一些私人项目。
In C# i can do something like this : 在C#中我可以这样做:
public class Test
{
public string A {get;set;}
public OtherClass B {get;set;}
}
public OtherClass ()
{
string Some {get;set;}
string Again {get;set;}
}
Now when I want to use class test i can do code like: 现在,当我想使用类测试时,我可以执行以下代码:
var test = new Test
{
A = "test",
B = new OtherClass
{
Some = "Some",
Again = "Again"
}
};
Thank to this now i have initialized test in clear way. 感谢现在我已经以明确的方式初始化了测试。
When i want do this in Java i must make code like : 当我想在Java中执行此操作时,我必须使代码如下:
public class Test
{
private string A;
public string GetA()
{
return A;
}
public void SetA(string a)
{
A = a;
}
privte OtherClass B;
public OtherClass GetB()
{
return B;
}
public void SetB(OtherClass b)
{
B = b;
}
}
public class OtherClass
{
private string Some;
public string GetSome()
{
return Some;
}
public void SetSome(string some)
{
Some = some;
}
privte string Again;
public string GetAgain()
{
return Again;
}
public void SetAgain(string again)
{
Again = again;
}
}
I know that Java must have Seter and Geter to field and i'm ok with this but now if i want to use Test
object in way like i use it in C# i must do something like : 我知道Java必须有Seter和Geter才能使用,但我现在可以使用它,但现在如果我想使用
Test
对象,就像我在C#中使用它一样,我必须做类似的事情:
OtherClass otherClass = new OtherClass();
otherClass.SetSome("Some");
otherClass.SetAnothier("Another");
Test test = new Test();
test.SetA("A")
test.SetB(otherClass);
IMO this is not nice and clear declaration. IMO这不是一个好的和明确的声明。 I know that i can add constructor like :
我知道我可以添加如下构造函数:
public Test(string a, otherClass b)
{
A = a;
B = b;
}
and 和
public OtherClass(string some,string another)
{
Some = some;
Another = another;
}
And use it like : 并使用它像:
Test test = new Test("Test",new OtherClass("some","Another"));
But when i have more complicated classes ( eg with 200 field ( yes i have that )) the constructor will be very long and it will hard to read it. 但是当我有更复杂的类(例如200字段(是的,我有))时,构造函数将很长并且很难读取它。
So can I initialize class in Java in other way that this I show you ? 那么我可以用我向你展示的其他方式在Java中初始化类吗?
Thank 谢谢
You can actually use object initializers
in java also, however they are called double brace initializers
. 实际上你也可以在java中使用
object initializers
,但是它们被称为double brace initializers
。 Example: 例:
Test test = new Test()
{{
SetA("test");
SetB(new OtherClass()
{{
SetSome("Some");
SetAgain("Again");
}});
}};
However you should probably not use this since it has some weird/unexpected side effects. 但是你可能不应该使用它,因为它有一些奇怪/意想不到的副作用。 This example produces an
AnonymousInnerClass
for Test
and OtherClass
. 此示例为
Test
和OtherClass
生成AnonymousInnerClass
。 You can read more about double brace initializers here . 您可以在此处阅读有关双支撑初始化器的更多信息
If you have more parameters than you wish to pass down your constructor you could use the builder pattern as suggested by this answer . 如果您有比您希望传递构造函数更多的参数,则可以使用此答案建议的构建器模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.