У меня есть два конструктора, которые передают значения полям только для чтения.
class Sample
{
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
_intField = i;
}
public Sample(int theInt)
{
_intField = theInt;
}
public int IntProperty
{
get { return _intField; }
}
private readonly int _intField;
}
Один конструктор получает значения напрямую, а другой выполняет некоторые вычисления и получает значения, а затем устанавливает поля.
Теперь вот catch:
Любые идеи?
Вот так:
public Sample(string str) : this(int.Parse(str)) {
}
Если то, что вы хотите, не может быть удовлетворительно достигнуто без инициализации в своем собственном методе (например, потому что вы хотите сделать слишком много до кода инициализации или обернуть его в try-finally или что-то еще), вы можете любой или все конструкторы передают переменные readonly по ссылке на процедуру инициализации, которая затем сможет манипулировать ими по желанию.
class Sample
{
private readonly int _intField;
public int IntProperty
{
get { return _intField; }
}
void setupStuff(ref int intField, int newValue)
{
intField = newValue;
}
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
setupStuff(ref _intField,i);
}
public Sample(int theInt)
{
setupStuff(ref _intField, theInt);
}
}
base(...)
или this(...)
мы можем выполнять только очень ограниченные операции.
Перед телом конструктора используйте:
: base (parameters)
: this (parameters)
Пример:
public class People: User
{
public People (int EmpID) : base (EmpID)
{
// Add more statements here.
}
}
Я улучшаюсь после ответа supercat. Я думаю, что также можно сделать следующее:
class Sample
{
private readonly int _intField;
public int IntProperty
{
get { return _intField; }
}
void setupStuff(ref int intField, int newValue)
{
//Do some stuff here based upon the necessary initialized variables.
intField = newValue;
}
public Sample(string theIntAsString, bool? doStuff = true)
{
//Initialization of some necessary variables.
//==========================================
int i = int.Parse(theIntAsString);
// ................
// .......................
//==========================================
if (!doStuff.HasValue || doStuff.Value == true)
setupStuff(ref _intField,i);
}
public Sample(int theInt): this(theInt, false) //"false" param to avoid setupStuff() being called two times
{
setupStuff(ref _intField, theInt);
}
}
new Sample(str, false)
.
Вот пример, который вызывает другой конструктор, затем проверяет его свойство.
public SomeClass(int i)
{
I = i;
}
public SomeClass(SomeOtherClass soc)
: this(soc.J)
{
if (I==0)
{
I = DoSomethingHere();
}
}
Да, вы можете вызвать другой метод раньше, чем база вызовов или это!
public class MyException : Exception
{
public MyException(int number) : base(ConvertToString(number))
{
}
private static string ConvertToString(int number)
{
return number.toString()
}
}
Когда вы наследуете класс из базового класса, вы можете вызвать конструктор базового класса, создав производный класс
class samle {
public int x;
public sample(int value) {
x = value;
}
}
class der : public sample {
public:
int a;
int b;
public der(int value1,int value2) : base(50) {
a = value1;
b = value2;
}
class run {
public static void main(String[] args){
der obj = new der(10,20);
Console.WriteLine(obj.x);
Console.WriteLine(obj.a);
Console.WriteLine(obj.b);
}
}
Вывод программы
50 10 20
Вы также можете использовать это ключевое слово для вызова конструктора из другого конструктора
class samle {
public int x;
public sample(int value) {
x = value;
}
public sample(sample obj):this(obj.x) {
}
}
class run{
public static void main(String[] args) {
sample s = new sample(20);
sample ss = new sample(s);
Console.WriteLine(ss.x);
}
}
Выход равен 20.
static
метод, который манипулирует параметрами.Sample(string)
будет выполнено первым, затемSample(int)
или версия int будет выполнена первой, а затем вернется к строковой версии? (Как вызовsuper()
в Java?)