B
B
borgez2014-03-06 19:33:23
C++ / C#
borgez, 2014-03-06 19:33:23

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

4 answer(s)
A
Alexander S, 2014-03-07
@FirstX

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;}

}

Now we go to the constructor and understand that the object is created only for the DSV, which is declared in the child class. In the parent, it was as empty as it was, and remains so, we did not say anything about it.
Now when we do this:
AutoCommon auto = new AutoA();
WriteLine(auto.dsv.a);
What do we see? That's right, nothing. Why? Yes, because we are trying to access a property that belongs to the parent class, and we never created it (more precisely, an object instance for the property), we created the property of the child object.
Let's rewrite the example like this:
public class autoA : autoCommon{
  public autoA(){
    base.dvs = new dvsDizel();
  }
     public new dvsDizel dvs {get;set;}

}

Now we create an instance of the object not for the child object, but for the parent.
After that, the example above will display what you apparently wanted, that is, a line with text.
BUT with this call:
AutoA auto = new AutoA();
WriteLine(auto.dsv.a);
You will again get an exception with null. Why ? Yes, because you are accessing the property of the child object, which this time we left empty)
Total: you have 2 properties in the child object (parent and "own") with the same name. To refer to the parent, use base.* , to refer to your own, you can explicitly use this.*, or this can be omitted.
public class autoA : autoCommon{
  public autoA(){
    base.dvs = new dvsDizel();
               this.dvs = base.dsv;
  }
     public new dvsDizel dvs {get;set;}

}

In this version, they now refer to the same object, but by accessing the parent data type you can get common dsv methods, and if you cast to a specific AutoA class, you can call DvsDizel methods.

K
Kokcuk, 2014-03-07
@Kokcuk

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
{
         ....
}

D
DancingOnWater, 2014-03-07
@DancingOnWater

In C#, multiple inheritance is not allowed.
What is the task?

G
gleb_kudr, 2014-03-08
@gleb_kudr

I poorly understood what you want, but I feel that here you need to use collections and relational relations, not classes. Your model just doesn't fit OOP inheritance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question