我在理解如何实现继承方面遇到了一些麻烦.
我的模型纯粹是出于演示的目的,但我们走了:
我有一个家长班Bus
.它有两个孩子DieselBus
和ElectricBus
.这些是抽象类.
我有另外两个班Coach
和CityBus
.我如何设置这些后,继承无论从DieselBus
OR 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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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 ());
}
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的程序执行有问题:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
无论我如何尝试,我都无法访问从相关总线继承引擎的任何类的任何属性.比如,我看不到Displacement
了的DieselEngine
我给Coach
"教练".
我还有两个课程Coach和CityBus.如何设置继承DieselBus或ElectricBus,而不必定义单独的Coach和CityBus类,如CoachDieselBus,CoachElectricBus,CityBusDieselBus和CityBusElectricBus?这甚至可行/可能吗?
不,那不可行.继承关系必须是固定的,因此Coach
必须始终具有相同的父类.
适合您的问题的解决方案是使用组合而不是继承:Coach
并且CityBus
都继承自Bus
.Bus
有一个Engine
属性,可以设置为a DieselEngine
或an ElectricEngine
.
(抱歉粗略的图表,我现在只有MS Paint可用.)