我在理解如何實現繼承時遇到了一些麻煩。解決C#中的繼承問題
我的模型是純粹爲示範的緣故,但在這裏我們去:
我有一個父類Bus
。它有兩個子女DieselBus
和ElectricBus
。這些是抽象類。
我還有兩類Coach
和CityBus
。我如何將這些設置爲從DieselBus
或ElectricBus
繼承,而不必定義單獨的Coach
和CityBus
類,如CoachDieselBus
,CoachElectricBus
,CityBusDieselBus
和CityBusElectricBus
?這甚至可行/可能嗎?
到目前爲止,我還沒有一些簡單的例子來尋找關於這一點的建議。
我所尋找的是這樣的:
與此相反:
謝謝!
編輯(2013年7月3日):
首先,一些代碼:
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);
}
}
}
無論我如何努力,我不能訪問任何的兩個類的屬性從相關的巴士繼承引擎。例如,我看不到DieselEngine
的Displacement
我給Coach
「教練」。
你有沒有考慮戰略設計模式? –
@VinayPandey我不熟悉那個概念 –
@capncoolio來訪問你只需要詢問對象獲取引擎的屬性,然後向引擎請求屬性示例:'coach.EngineType.Power' – WiiMaxx