我有一種感覺,我做錯了。我是新來的抽象類等,一直在讀一些教程,但我無法弄清楚如何將它應用於我的情況。我認爲我的設計可能有問題,但我想不出另一種方式來做到這一點。我的公司生產一些不同的電腦,我需要能夠監控電池信息。儘管獲取信息不是問題,但要弄清楚如何將不同的命令發送到基類來完成我需要的操作。說我想讓我的電池1電壓。在一個單元上,命令是0x0418,另一個單元是0x453。所以在我的信息課上,我運行一個測試來看看模型是什麼。我有一個稱爲電池的基類,它有一堆變量,這些變量是每個電池的標準變量(電池電壓,充電電流,充電電流等),然後決定爲我的每個單元制定延長電池的單獨班級。從繼承類傳遞給基類的方法
現在我的設計我認爲是現貨(可能是錯的,因爲我不擅長抽象和多態)。我有一個面板,最終會顯示我從BatteryInformation類獲得的信息。像Battery1Cell1Label.Text = batteryInfo.GetCell1(1); Battery2Cell1Label = batteryInfo.GetCell1(2)。我想我需要一個GetValue(字節命令)(因爲它是一個嵌入式控制器命令來獲取每種不同類型的信息)。也許我應該停止說話,只是發佈我的代碼我有什麼並告訴你我有錯誤。
battery.cs
public abstract class Battery<T> //not sure that the <T> is right
{
public string Information { get; private set; }
public float Cell1 { get; private set; }
public float Cell2 { get; private set; }
public float Cell3 { get; private set; }
public float Cell4 { get; private set; }
public int FCC { get; private set; }
public bool ChargeIC { get; private set; }
public int StartCharge { get; private set; }
public int CurrentCharge { get; private set; }
public bool Exists { get; private set; }
protected internal void GetValue(byte command)
{
//Use Embedded controller to get said value
//ECPort.ReadEC(command);
//Testing Purposeses
Console.WriteLine(command);
}
}
Battery8800.cs
class Battery8800 : Battery<Battery8800>
{
public Battery8800() : base()
{
}
public void GetValue(BatteryCommands command)
{
base.GetValue((byte)command);
}
public enum BatteryCommands
{
Battery1VoltageHigh = 0x0402,
Battery1VoltageLow = 0x0403,
Batt1ChargeCurrentHigh = 0x0404,
Batt1ChargeCurrentLow = 0x0405,
Battery1MaxError = 0x0407,
Battery1RSOC = 0x0409,
Battery1FCCHigh = 0x040E,
Battery1FCCLow = 0x040F,
Battery1DCHigh = 0x0412,
Battery1DCLow = 0x0413,
Battery1Cell1High = 0x0418,
Battery1Cell1Low = 0x0419,
Battery1Cell2High = 0x041A,
Battery1Cell2Low = 0x041B,
Battery1Cell3High = 0x041C,
Battery1Cell3Low = 0x041D,
Battery1Cell4High = 0x041E,
Battery1Cell4Low = 0x041F,
PowerSource1 = 0x0420,
//many more commands for battery 2 etc etc
}
}
BatteryInformation.cs
class BatteryInformation
{
public Battery battery1; //error says it needs 1 type of argument
public Battery battery2; //error says it needs 1 type of argument
public BatteryInformation()
{
switch (UnitModel.GetModelEnum())
{
case UnitModel.DLIModel.DLI8300M:
battery1 = new Battery8300();
battery2 = new Battery8300();
break;
case UnitModel.DLIModel.DLI8400:
battery1 = new Battery8400();
battery2 = new Battery8400();
break;
case UnitModel.DLIModel.DLI8500:
battery1 = new Battery8500();
break;
case UnitModel.DLIModel.DLI8500P:
battery1 = new Battery8500P();
break;
case UnitModel.DLIModel.DLI8800:
battery1 = new Battery8800();
break;
case UnitModel.DLIModel.DLI9200:
battery1 = new Battery9200();
break;
default:
break;
}
//for testing purposes
battery1 = new Battery8800();
battery1.DoThis(Battery8800.BatteryCommands.Batt1ChargeCurrentHigh);
}
}
YEAH在尾節能!權力剛剛熄滅了,我沒有放鬆,但一句話!
所以,當我的電腦開機時,我想在我的電池板類中做這樣的事情可能會更好。
//in my timer_tick event
BatteryInformation.UpdateBatteries();
battery1Cell1Label.Text = BatteryInformation.Battery1.Cell1.ToString();
//etc etc
,但我仍然需要得到這個工作,但我有一個很難搞清楚該怎麼做抽象。感謝您的時間。
編輯
我想我會對此錯誤的方式。
class Battery1_8400 : Battery
{
public override bool Update()
{
//TODO finish
Exists = GetValue((ushort)Commands.PowerSource) != 0xFF;
if (Exists)
{
Cell1 = GetValue((ushort)Commands.Cell1Low, (ushort)Commands.Cell1High)/1000.0f;
Cell2 = GetValue((ushort)Commands.Cell2Low, (ushort)Commands.Cell2High)/1000.0f;
Cell3 = GetValue((ushort)Commands.Cell3Low, (ushort)Commands.Cell3High)/1000.0f;
FCC = GetValue((ushort)Commands.FCCLow, (ushort)Commands.FCCHigh);
Voltage = GetValue((ushort)Commands.VoltageLow, (ushort)Commands.VoltageHigh);
return true;
}
else
{
return false;
}
}
private enum Commands
{
PowerSource = 0x0480,
Charge = 0x0432,
RSOC = 0x0734,
DCLow = 0x0402,
DCHigh = 0x0403,
FCCLow = 0x0404,
FCCHigh = 0x0405,
MaxError = 0x0730,
Cell1Low = 0x0778,
Cell1High = 0x0779,
Cell2Low = 0x077C,
Cell2High = 0x077D,
Cell3Low = 0x0780,
Cell3High = 0x0781,
VoltageLow = 0x0438,
VoltageHigh = 0x0439,
ChargeCurrentLow = 0x0728,
ChargeCurrentHigh = 0x0729,
ChargeIC = 0x1A03,
}
}
我有9個文件在更新命令的工作方式方面都是相同的,不同之處在於enum。每個類的命令略有不同。看看batter2_8400.cs的枚舉
private enum Commands
{
PowerSource = 0x0480,
Charge = 0x04C2,
RSOC = 0x0834,
DCLow = 0x0492,
DCHigh = 0x0493,
FCCLow = 0x0494,
FCCHigh = 0x0495,
MaxError = 0x0830,
Cell1Low = 0x0878,
Cell1High = 0x0879,
Cell2Low = 0x087C,
Cell2High = 0x087D,
Cell3Low = 0x0880,
Cell3High = 0x0881,
VoltageLow = 0x04C8,
VoltageHigh = 0x04C9,
ChargeCurrentLow = 0x0828,
ChargeCurrentHigh = 0x0829,
ChargeIC = 0x1A04,
}
更新命令是一樣的,以及其他7個文件。似乎有點不好的設計給我,但我很難理解我應該如何做到這一點。順便說一句,這是我的課程後,我給了一個答案,並收到少數意見。
什麼是正在使用的通用參數?它看起來像你,只是不需要它。 –
2012-04-26 16:10:44
是Cell1應該調用getvalue什麼的? – 2012-04-26 16:20:44
@JamieDixon通用來自我試圖從其他職位複製一些關於繼承的想法。 –
2012-04-26 16:42:11