3
I own the class Carro
who inherits from the class Objeto
and another class CarroTeste
implementing the abstract class TesteBase
that by its implements the interface ITeste
, both (TesteBase
and ITeste
) typed with restriction to use only class objects Objeto
, the problem occurs when I try to instantiate a CarroTeste
in an Iteste variable ITeste<Objeto> teste = new CarroTeste();
, IDE says I can’t do an implicit type conversion.
The class code is below.
public class Objeto
{
protected string _nome;
public string nome { get{return _nome;} }
}
public class Carro : Objeto
{
public Carro()
{
_nome = "Carro";
}
}
public interface ITeste<T> where T : Objeto
{
string GetNome();
List<T> List { get; }
T GetChange(T obj);
}
public abstract class TesteBase<T> : ITeste<T> where T : Objeto
{
protected Objeto _obj = null;
public abstract string GetNome();
public abstract List<T> List { get; }
public abstract T GetChange(T obj);
}
public class CarroTeste : TesteBase<Carro>
{
public override string GetNome()
{
return "Meu nome é : " + _obj.nome;
}
public override List<Carro> List
{
get { return new List<Carro>(); }
}
public override Carro GetChange(Carro obj)
{
return obj;
}
}
During my tests with the answer given by bigwon I changed the interface to use the modifier
out
and implemented in the classCarroTeste
a method calledCarroToObjeto
that through functionConvertAll
of ownList
returns me a new list with the correct type, but obviously a List<Object> does not have all Car properties and methods.CarroTeste
instead of the interfaceITeste
.– Benjamim Mendes Junior