我在理解如何实现继承时遇到了一些麻烦。

我的模型纯粹是为了演示,但在这里我们开始:

我有一个家长课程Bus。它有两个子DieselBusElectricBus。这些是抽象类。

我还有另外两个类CoachCityBus。如何设置那些继承自DieselBusElectricBus的类,而不必定义单独的CoachCityBus类,例如CoachDieselBusCoachElectricBusCityBusDieselBusCityBusElectricBus?那甚至可行/可能吗?

到目前为止,我还没有一些例子,只是寻求一些建议。

我正在寻找的是:


与此相反:


谢谢!

编辑(2013-07-03):

首先,一些代码:

公交车

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 {
            get {
                return id;
            }
            set {
                id = value;
            }
        }

        private int seats;
        public int Seats {
            get {
                return seats;
            }
            set {
                seats = value;
            }
        }

        private float length;
        public float Length {
            get {
                return length;
            }
            set {
                length = value;
            }
        }

        private String colour;
        public String Colour {
            get {
                return colour;
            }
            set {
                colour = value;
            }
        }
    }
}


教练

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 {
            get {
                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 {
            get {
                return power;
            }
            set {
                power = value;
            }
        }

        private float torque;
        public float Torque {
            get {
                return torque;
            }
            set {
                torque = value;
            }
        }

        private int redline;
        public int Redline {
            get {
                return redline;
            }
            set {
                redline = value;
            }
        }
    }
}


柴油发动机

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 {
            get {
                return displacement;
            }
            set {
                displacement = value;
            }
        }

        private float fuelEconomy;
        public float FuelEconomy {
            get {
                return fuelEconomy;
            }
            set {
                fuelEconomy = value;
            }
        }

        public override string ToString () {
            return "Diesel Engine";
        }
    }
}


我在程序执行中遇到问题:

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


无论如何尝试,我都无法访问从其相关总线继承引擎的任何一个类的任何属性。例如,我看不到我给Displacement“教练”提供的DieselEngineCoach

最佳答案

我还有另外两个班级Coach和CityBus。如何设置那些继承自DieselBus或ElectricBus的类,而不必定义单独的Coach和CityBus类,例如CoachDieselBus,CoachElectricBus,CityBusDieselBus和CityBusElectricBus?那甚至可行/可能吗?


不,那是不可行的。继承关系必须是固定的,因此Coach必须始终具有相同的父类。

针对您的问题的适当解决方案是使用组合而不是继承:CoachCityBus都从Bus继承。 Bus具有Engine属性,可以将其设置为DieselEngineElectricEngine



(对不起的示意图,对不起,我现在只有MS Paint可用。)

10-04 19:13