gpt4 book ai didi

c# - 在 C# 中解决继承问题

转载 作者:行者123 更新时间:2023-11-30 19:16:42 24 4
gpt4 key购买 nike

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

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

我有一个父类 Bus。它有两个 child DieselBusElectricBus。这些是抽象类。

我还有两个类 CoachCityBus。如何将它们设置为从 DieselBusElectricBus 继承,而无需定义单独的 CoachCityBus 类像 CoachDieselBusCoachElectricBusCityBusDieselBusCityBusElectricBus 吗?这是否可行/可能?

到目前为止,除了一些骨架,我没有示例,正在寻求有关这一点的建议。

我要找的是这个: Correct

与此相反: Incorrect

谢谢!

编辑(2013-07-03):

首先,一些代码:

总线.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;
}
}
}
}

教练.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 ());
}
}
}

引擎.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);
}
}
}

无论我如何尝试,我都无法访问从相关总线继承引擎的任一类的任何属性。例如,我看不到 DieselEngineDisplacement 我给了 Coach “coach”。

最佳答案

I have a further two classes Coach and CityBus. How do I set those up to inherit from either DieselBus OR ElectricBus without having to define separate Coach and CityBus classes like CoachDieselBus, CoachElectricBus, CityBusDieselBus and CityBusElectricBus ? Is that even feasible/possible?

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

您的问题的适当解决方案是使用组合而不是继承:CoachCityBus 都继承自BusBus 有一个 Engine 属性,可以将其设置为 DieselEngineElectricEngine

Diagram

(对不起,粗略的图表,我现在只有 MS Paint 可用。)

关于c# - 在 C# 中解决继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com