Having issues addressing inheritance in C# -


i'm having trouble understanding how implement inheritance.

my model purely demonstration's sake, here go:

i have parent class bus. has 2 children dieselbus , electricbus. these abstract classes.

i have further 2 classes coach , citybus. how set inherit either dieselbus or electricbus without having define separate coach , citybus classes coachdieselbus, coachelectricbus, citybusdieselbus , citybuselectricbus ? feasible/possible?

i don't have examples short of skeletons far, looking advice on point.

what looking this: correct

as opposed this: incorrect

thanks!

edit (2013-07-03):

first, code:

bus.cs

using system; using system.collections.generic; using system.linq; using system.text;  namespace inheritancetest {     abstract class bus {         public bus () {          }          public abstract engine enginetype {             get;             set;         }          private int id;         public int id {             {                 return id;             }             set {                 id = value;             }         }          private int seats;         public int seats {             {                 return seats;             }             set {                 seats = value;             }         }          private float length;         public float length {             {                 return length;             }             set {                 length = value;             }         }          private string colour;         public string colour {             {                 return colour;             }             set {                 colour = value;             }         }     } } 

coach.cs

using system; using system.collections.generic; using system.linq; using system.text;  namespace inheritancetest {     class coach : bus {         public coach (int id, int seats, float length, string colour, string company, engine engine) {             this.id = id;             this.seats = seats;             this.length = length;             this.colour = colour;             this.enginetype = engine;             this.company = company;         }          public override engine enginetype {             get;             set;         }          private string company;         public string company {             {                 return company;             }             set {                 company = value;             }         }          public override string tostring () {             return (this.id + "\t" + this.seats + "\t" + this.length + "\t" + this.colour + "\t" + this.company + "\t" + this.enginetype.tostring ());         }     } } 

engine.cs

using system; using system.collections.generic; using system.linq; using system.text;  namespace inheritancetest {     abstract class engine {         public engine () {          }          private float power;         public float power {             {                 return power;             }             set {                 power = value;             }         }          private float torque;         public float torque {             {                 return torque;             }             set {                 torque = value;             }         }          private int redline;         public int redline {             {                 return redline;             }             set {                 redline = value;             }         }     } } 

dieselengine.cs

using system; using system.collections.generic; using system.linq; using system.text;  namespace inheritancetest {     class dieselengine : engine {         public dieselengine (float power, float torque, int redline, float displacement, float fueleconomy) {             this.power = power;             this.torque = torque;             this.redline = redline;             this.displacement = displacement;             this.fueleconomy = fueleconomy;         }          private float displacement;         public float displacement {             {                 return displacement;             }             set {                 displacement = value;             }         }          private float fueleconomy;         public float fueleconomy {             {                 return fueleconomy;             }             set {                 fueleconomy = value;             }         }          public override string tostring () {             return "diesel engine";         }     } } 

i'm having problem in program execution:

using system; using system.collections.generic; using system.linq; using system.text;  namespace inheritancetest {     class program {         static void main (string[] args) {             string line;             var diesel = new dieselengine (200f, 550f, 3500, 7.5f, 10f);             var electric = new electricengine (85f, 1000f, 19000, 24f, 65000f);             var city = new citybus (124, 75, 18.5f, "red", "brisbane", electric);             var coach = new coach (777, 120, 22.5f, "blue", "greyhound", diesel);             line = new string ('-', city.tostring ().length * 2);              system.console.writeline ("city buses\n\nid\tseats\tlength\tcolour\tcity\t\tengine type");             system.console.writeline (line);             system.console.writeline (city.tostring ());              system.console.writeline ("\n\ncoach buses\n\nid\tseats\tlength\tcolour\tcompany\t\tengine type");             system.console.writeline (line);             system.console.writeline (coach.tostring ());              system.console.readkey (true);         }     } } 

no matter how try to, cannot access of properties of either of classes inherit engine relevant buses. example, cannot see displacement of dieselengine gave coach "coach."

i have further 2 classes coach , citybus. how set inherit either dieselbus or electricbus without having define separate coach , citybus classes coachdieselbus, coachelectricbus, citybusdieselbus , citybuselectricbus ? feasible/possible?

no, that's not feasible. inheritance relation must fixed, coach must always have same parent classes.

the appropriate solution problem use composition instead of inheritance: coach , citybus both inherit bus. bus has engine property, can set either dieselengine or electricengine.

diagram

(sorry crude diagram, had ms paint available right now.)


Comments

Popular posts from this blog

postgresql - Lazarus + Postgres: incomplete startup packet -

c# - How to get the current UAC mode -