int a, b, c;
Constructor()
{
a = 5;
b = 10;
c = 15;
//do stuff
}
Constructor(int x, int y)
{
a = x;
b = y;
c = 15;
//do stuff
}
Constructor(int x, int y, int z)
{
a = x;
b = y;
c = z;
//do stuff
}
Чтобы предотвратить дублирование "материала" и нескольких заданий, я попробовал что-то вроде:
int a, b, c;
Constructor(): this(5, 10, 15)
{
}
Constructor(int x, int y): this(x, y, 15)
{
}
Constructor(int x, int y, int z)
{
a = x;
b = y;
c = z;
//do stuff
}
Это работает для того, что я хочу делать, но иногда мне нужно использовать какой-то длинный код для создания новых объектов или выполнения некоторых вычислений:
int a, b, c;
Constructor(): this(new Something(new AnotherThing(param1, param2, param3),
10, 15).CollectionOfStuff.Count, new SomethingElse("some string", "another
string").GetValue(), (int)Math.Floor(533 / 39.384))
{
}
Constructor(int x, int y): this(x, y, (int)Math.Floor(533 / 39.384))
{
}
Constructor(int x, int y, int z)
{
a = x;
b = y;
c = z;
//do stuff
}
Этот код почти такой же, как и раньше, только те параметры, которые передаются, не очень читаемы. Я бы предпочел сделать что-то вроде:
int a, b, c;
Constructor(): this(x, y, z) //compile error, variables do not exist in context
{
AnotherThing at = new AnotherThing(param1, param2, param3);
Something st = new Something(aThing, 10, 15)
SomethingElse ste = new SomethingElse("some string", "another string");
int x = thing.CollectionOfStuff.Count;
int y = ste.GetValue();
int z = (int)Math.Floor(533 / 39.384);
//In Java, I think you can call this(x, y, z) at this point.
this(x, y, z); //compile error, method name expected
}
Constructor(int x, int y): this(x, y, z) //compile error
{
int z = (int)Math.Floor(533 / 39.384);
}
Constructor(int x, int y, int z)
{
a = x;
b = y;
c = z;
//do stuff
}
В основном я строю параметры внутри тела конструктора. Затем я пытаюсь передать эти встроенные параметры другому конструктору. Я думаю, что я помню, как можно было использовать ключевые слова "this" и "super" для вызова конструкторов, а внутри тела другого конструктора при кодировании на Java. Это не похоже на С#.
Есть ли способ сделать это легко? Я сделал что-то неправильно? Если это невозможно, я должен просто придерживаться нечитаемого кода?
Думаю, я всегда мог вырезать дублированный код в другой метод, полностью вне конструкторов. Тогда каждый конструктор просто выполнит свою собственную задачу и вызовет код, разделяемый другими конструкторами.