Answer the question
In order to leave comments, you need to log in
C# - how to organize multiple inheritance?
There are Classes with inheritance, how to make it so that it does not produce null for the dvs field at runtime and override does not allow using the inherited dvsDizel class?
autoA
dvs null
dvs dvsDizel
a dvs
autoB
dvs null
dvs dvsBenzin
a dvs
Run in linqpad e.g.
void Main()
{
var a = new autoA();
a.Dump();
var b = new autoB();
b.Dump();
}
// Define other methods and classes here
public class dvsCommon{
public dvsCommon(){
a="dvs";
}
public string a{get;set;}
}
public class dvsBenzin : dvsCommon{
public dvsBenzin() : base(){
}
}
public class dvsDizel : dvsCommon{
public dvsDizel() : base(){
}
}
public class autoCommon{
public dvsCommon dvs {get;set;}
}
public class autoA : autoCommon{
public autoA(){
dvs = new dvsDizel();
}
public dvsDizel dvs {get;set;}
}
public class autoB : autoCommon{
public autoB(){
dvs = new dvsBenzin();
}
public dvsBenzin dvs {get;set;}
}
Answer the question
In order to leave comments, you need to log in
The task is of course hard, it is really very difficult to understand the ultimate goal of your manipulations (and the motivation for what you are doing this). But a little theory about what's going on here.
There is an AutoCommon and there are a number of descendants AutoCommon has a DSV property and AutoA again has a DSV property definition. At this point, the compiler implicitly considers that this is a completely new property, not related to the parent property in any way (each one remains with its own property). That is, what you wrote actually looks like this
public class autoA : autoCommon{
public autoA(){
this.dvs = new dvsDizel();
}
public new dvsDizel dvs {get;set;}
}
public class autoA : autoCommon{
public autoA(){
base.dvs = new dvsDizel();
}
public new dvsDizel dvs {get;set;}
}
public class autoA : autoCommon{
public autoA(){
base.dvs = new dvsDizel();
this.dvs = base.dsv;
}
public new dvsDizel dvs {get;set;}
}
Well, in this case, it would not be bad to do something like this, if I understand correctly:
public interface IAuto
{
IEngine Engine {get;}
}
public AutoA: IAuto
{
Engine { return new Dizel(); }
}
public interface IEngine
{
....
}
public class Dizel : IEngine
{
....
}
In C#, multiple inheritance is not allowed.
What is the task?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question