Vi posto questo esempio più o meno semplice per comprendere un filo meglio come
si comportano e le brevi differenze che ci possono essere fra l'una e l'altra.
public interface iTestClass
{
string ReturnMeString();
int ReturnMeInt();
string ReturnBasedString();
}
public class VirtualTest : iTestClass
{
public virtual string ReturnMeString()
{
return "virtual base ";
}
public virtual int ReturnMeInt()
{
return 0;
}
public virtual string ReturnBasedString()
{
return "virtual base ";
}
}
public abstract class AbstractTest :iTestClass
{
public abstract string ReturnMeString();
public abstract int ReturnMeInt();
public virtual string ReturnBasedString()
{
return "abstract base ";
}
}
public class ConcreteTest1 : AbstractTest
{
public override string ReturnMeString()
{
return "Concrete Test";
}
public override int ReturnMeInt()
{
return -1;
}
public override string ReturnBasedString()
{
return base.ReturnBasedString() + " New from concrete";
}
}
public class ConcreteTest2 : VirtualTest
{
public override string ReturnMeString()
{
return "Concrete Test";
}
public override int ReturnMeInt()
{
return -1;
}
public override string ReturnBasedString()
{
return base.ReturnBasedString() + " New from concrete";
}
}
public class testAll
{
public testAll()
{
}
public void DoTest()
{
iTestClass ct1 = new ConcreteTest1();
iTestClass ct2 = new ConcreteTest2();
Console.WriteLine("Ct1 String:" + ct1.ReturnMeString());
Console.WriteLine("Ct1 int:" + ct1.ReturnMeInt());
Console.WriteLine("Ct1 Base:" + ct1.ReturnBasedString());
Console.WriteLine("Ct2 String:" + ct2.ReturnMeString());
Console.WriteLine("Ct2 int:" + ct2.ReturnMeInt());
Console.WriteLine("Ct2 Base:" + ct2.ReturnBasedString());
Console.ReadLine();
}
}
Di certo e come sempre sta a noi individuare il contesto migliore per toglierci dai
guai o dai casini.
Nessun commento:
Posta un commento