English 中文(简体)
从继承类别到基类的方法
原标题:methods from inherited class passed to base class

我感到我错了。 我是新到抽象的课堂,而且已经读过几门的辅导,但我可以说明如何适用于我的情况。 我认为,我的设计可能会有错,但我认为这样做还有另一种方式。 我的公司配备了几台不同的电脑,我需要能够监测电池信息。 虽然获取信息不是问题,但将如何将不同的指挥权派到基地类别去做需要做的事情。 Say 我想获得我的第1号囚室。 在一个部队中,指挥部为0x0418,而另一个单位为0x453。 因此,在我的信息类别中,我进行了一项测试,以了解这一模式是什么。 我有一个叫做“电池”的基地级,每个电池的标准是一组变数(电池、电荷、电荷等)。 我随后决定,为我所属的每个单位开设个人班是好的。

Now my design of the classes I think is spot on (could be wrong as I am not good at abstraction and polymorphism). I have a panel that in the end would display the information that I get from the BatteryInformation class. something like 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);
    }
}

页: 1

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 FOR DRAFT SAVING!!! the power just went out, and I didn t loose but 1 sentence!

因此,虽然我的计算机正在转回我,但我认为,在我的电池组别中,这样做可能更好。

    //in my timer_tick event
    BatteryInformation.UpdateBatteries();
    battery1Cell1Label.Text = BatteryInformation.Battery1.Cell1.ToString();
    //etc etc

but i still need to get this working but am having a hard time figuring out how to do abstraction. Thank you for your time.

http://www.ohchr.org。

我认为,这样做是错误的。

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份档案,在最新指挥系统如何运作时,这些档案完全相同。 各级指挥略有不同。 look2 - 8400.cs s enum

    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份档案相同。 种子对我来说是坏的,但我如何做这种 design。 顺便提一下我的班子在答复我之后所看到的情况,以及收到的少数评论。

最佳回答

最后,我认为,我阐述了我应该做些什么。 这对我来说是干净的,看来是明智的。 你们是否可以批评吗?

public abstract class Battery
{
    public string Information { get; set; }
    public float Cell1 { get {return GetValue(Cell1Low, Cell1High) / 1000.0f;} }
    public float Cell2 { get {return GetValue(Cell2Low, Cell2High) / 1000.0f;} }
    public float Cell3 { get {return GetValue(Cell3Low, Cell3High) / 1000.0f;} }
    public float Cell4 { get {return GetValue(Cell4Low, Cell4High) / 1000.0f;} }
    public float Voltage { get {return GetValue(VoltageLow, VoltageHigh);} }
    public int DC { get {return GetValue(DCLow, DCHigh);} }
    public int FCC { get {return GetValue(FCCLow, FCCHigh);} }
    //public bool ChargeIC { get {return } }
    //public int StartCharge { get {return } }
    //public int CurrentCharge { get {return } }
    public bool Exists { get {return GetValue(PowerSource) != 0xFF} }
    public int FCCPercent { get {return ((FCC * 100) / DC);} }

    /// <summary>
    /// Gets a value depending on the Embedded controller
    /// </summary>
    /// <param name="low">The low byte command to process</param>
    /// <param name="high">The high byte command to process</param>
    /// <returns></returns>
    private int GetValue(ushort low, ushort high)
    {
        //Use Embedded controller to get said value
        //ECPort.ReadEC(command);
        //Testing Purposeses
        var lowValue = ECPort.ReadEC(low);
        var highValue = ECPort.ReadEC(high);
        return (int)((highValue << 8) + lowValue);
    }

    private int GetValue(ushort command)
    {
        return (int)ECPort.ReadEC(command);
    }

    public abstract ushort PowerSource {get;}
    public abstract ushort Charge{get;}
    public abstract ushort RSOC{get;}
    public abstract ushort DCLow{get;}
    public abstract ushort DCHigh{get;}
    public abstract ushort FCCLow{get;}
    public abstract ushort FCCHigh{get;}
    public abstract ushort MaxError{get;}
    public abstract ushort Cell1Low{get;}
    public abstract ushort Cell1High{get;}
    public abstract ushort Cell2Low{get;}
    public abstract ushort Cell2High{get;}
    public abstract ushort Cell3Low{get;}
    public abstract ushort Cell3High{get;}
    public abstract ushort Cell4Low { get; }
    public abstract ushort Cell4High { get; }
    public abstract ushort VoltageLow{get;}
    public abstract ushort VoltageHigh{get;}
    public abstract ushort ChargeCurrentLow{get;}
    public abstract ushort ChargeCurrentHigh{get;}
    public abstract ushort ChargeIC{get;}

}

然后,在我继承的分班上,就是这样的例子。

class Battery1_8400 : Battery
{
    public override ushort PowerSource { get {return 0x0480;}}
    public override ushort Charge { get {return  0x0432;}}
    public override ushort RSOC { get {return  0x0734;}}
    public override ushort DCLow { get {return  0x0402;}}
    public override ushort DCHigh { get {return  0x0403;}}
    public override ushort FCCLow { get {return  0x0404;}}
    public override ushort FCCHigh { get {return  0x0405;}}
    public override ushort MaxError { get {return  0x0730;}}
    public override ushort Cell1Low { get {return  0x0778;}}
    public override ushort Cell1High { get {return  0x0779;}}
    public override ushort Cell2Low { get {return  0x077C;}}
    public override ushort Cell2High { get {return  0x077D;}}
    public override ushort Cell3Low { get {return  0x0780;}}
    public override ushort Cell3High { get {return  0x0781;}}
    public override ushort VoltageLow { get {return  0x0438;}}
    public override ushort VoltageHigh { get {return  0x0439;}}
    public override ushort ChargeCurrentLow { get {return  0x0728;}}
    public override ushort ChargeCurrentHigh { get {return  0x0729;}}
    public override ushort ChargeIC { get {return  0x1A03;}}
}

现在必须做的是一只档案电池。 c 如果需要改动。

问题回答

页 次 电池信息应当具有你所需要的每一价值的抽象特性:

public abstract class BatteryInfo {
    // int should be replaced with the actual data type of the value
    public abstract int VoltageHigh { get; }
    public abstract int VoltageLow { get; }
    // etc. for each value you need
}

然后,在子女类别中,你执行每一项财产。

public class Battery8800 : BatteryInfo {
    public override int VoltageHigh {
        get {
          int value;
          // do code to retrieve value
          return value;
        }
    }
}

此外,我还要提供一种方法,使贵组织能够消费以下方法:

 public IEnumerable<BatteryInfo> GetAllBatteryInfo() {
   // get each battery
 }

That way the UI does not need to worry about how the battery info is retrieved for each battery. That allows you to use one of the list or grid style controls to view the battery info.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签