我在理解如何实现继承方面遇到了一些麻烦。
我的模型纯粹是出于示范的目的,但我们走了:
我有一个家长班Bus
。它有两个孩子DieselBus
和ElectricBus
。这些是抽象类。
我还有两个班级Coach
和CityBus
。如何将这些内容设置为从DieselBus
或ElectricBus
继承而不必定义单独的Coach
和CityBus
类,例如CoachDieselBus
,CoachElectricBus
,CityBusDieselBus
和CityBusElectricBus
?这甚至可行/可能吗?
到目前为止,我没有关于一些骷髅的例子,在这一点上寻求建议。
我在寻找的是:
与此相反:
谢谢!
编辑(2013-07-03):
首先,一些代码:
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 {
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;
}
}
}
}
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 {
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;
}
}
}
}
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 {
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
DieselEngine
给Coach
“教练的{{1}}。”
答案 0 :(得分:6)
我还有两个课程Coach和CityBus。如何设置继承DieselBus或ElectricBus,而不必定义单独的Coach和CityBus类,如CoachDieselBus,CoachElectricBus,CityBusDieselBus和CityBusElectricBus?这甚至可行/可能吗?
不,那不可行。继承关系必须已修复,因此Coach
必须始终具有相同的父类。
针对您的问题的适当解决方案是使用合成而不是继承:Coach
和CityBus
都继承自Bus
。 Bus
具有Engine
属性,可以设置为DieselEngine
或ElectricEngine
。
(对不起粗略的图表,我现在只有MS Paint可用。)
答案 1 :(得分:2)
公共汽车有一个发动机,它是DieselEngine和ElectricEngine的基础。
公共汽车的用法可能是City and Coach的基类。
这里使用继承是一种经典的反模式。一个典型的初学者错误 - 所以不要为此感到难过。结果主要是太复杂的继承树。
您也可以使用接口(IDieselEngine,IElectricEngine),但我可能会使用它。
答案 2 :(得分:1)
在C#中,您只能从一个类继承,因此只能使用第二个图中显示的类。
您可以使用接口,因为类可以实现多个接口,但接口应该更多地用于定义行为,而不是类和属性/质量的类和柴油/电动和城市/教练看起来更像是公共汽车的属性。 / p>
答案 3 :(得分:1)
这是一个如何做你想做的事的例子
public abstract class Engine
{
public abstract string Name { get; set; }
}
public class DieselEngine:Engine
{
public override string Name { get; set; }
public string DieselEngineProperty { get; set; }
}
public class ElectricEngine : Engine
{
public override string Name { get; set; }
public string ElectricEngineProperty { get; set; }
}
public class HybridEngine : Engine
{
public override string Name { get; set; }
public string HybridEngineProperty { get; set; }
public List<Engine> EngineList { get; set; }
}
public abstract class Bus
{
public abstract string Name { get; set; }
public abstract string Lengh { get; set; }
public abstract Engine BusEngine { get; set; }
}
public class CoachBus: Bus
{
public override string Name { get; set; }
public string CoachBusProperty { get; set; }
}
public class CityBus:Bus
{
public override string Name { get; set; }
public override string Lengh { get; set; }
public override Engine BusEngine { get; set; }
public string CityBusProperty { get; set; }
}
这里有一些巴士
var myDieselEngine = new DieselEngine();
myDieselEngine.Name = "TöffTöff";
var myElectricEngine = new ElectricEngine();
myDieselEngine.Name = "SumSum";
var myHybridEngine = new HybridEngine();
myDieselEngine.Name = "SumTöff";
var myDieselCityBus = new CityBus();
myDieselCityBus.Name = "Shorty1";
myDieselCityBus.Lengh = "5 m";
myDieselCityBus.BusEngine = myDieselEngine;
var myElectricCityBus = new CityBus();
myElectricCityBus.Name = "Shorty2";
myElectricCityBus.Lengh = "7 m";
myElectricCityBus.BusEngine = myElectricEngine;
var myHybridCoachBus = new CoachBus();
myHybridCoachBus.Name = "Shorty2";
myHybridCoachBus.Lengh = "15 m";
myHybridCoachBus.BusEngine = myHybridEngine;