Вот мой пример: < /p>
Код: Выделить всё
B b = new B(10, 20);
Print(b);
void Print(T t) where T : A
{
Console.WriteLine(t.Get());
}
class A
{
protected int a;
public A(int a)
{
this.a = a;
}
public A Get()
{
return this;
// return new A(a);
}
public override string ToString()
{
return $"{a}";
}
}
class B : A
{
int b;
public B(int a, int b) : base(a)
{
this.b = b;
}
public B Get()
{
return this;
}
public override string ToString()
{
return $"{a}, {b}";
}
}
Но если мы заменим возвращение этого с возвратом новой a (a) в методе get () базового класса A , в этом случае, в результате, приведенный в этом случае «10». Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/795 ... lts-for-my