1

私はいくつかの電気回路網シミュレーション ソフトウェア ( ElecNetKit ) に取り組んできました。電気回路網では、単相モデルで作業する方が便利な場合もあれば、三相モデルで作業する方が便利な場合もあります。

そのため、電気ネットワーク要素の 1 つを次のように表現できるようにしたいと考えています。

class Bus
{
    public Complex Voltage {set; get;} //single phase property
}

しかし同時に、ユーザーが を呼び出して、有効な整数に対してBus.Voltage.Phases[x]a を期待できるような方法で。Complexx

として扱われる場合、プロパティBus.Voltageは にマップする必要があります。Bus.Voltage.Phases[1]Complex

ここで 2 つの質問があります。

  1. これは OOP の原則に違反していますか? ありそうな予感。
  2. これを C# で表現する便利な方法はありますか?

表現に関して、私は試しました:

  • classPhased<T> : Tですが、これは型付けシステムと互換性がありません。
  • Phased<T>type への汎用コンバーターを持つクラスTですが、コンバーターを呼び出す必要があります。

次のようなものを簡単に使用できることを認識しています。

public Dictionary<int,Complex> VoltagePhases {private set; get;}
public Complex Voltage {
    set {VoltagePhases[1] = value;} 
    get {return VoltagePhases[1];}
}

ただし、複数のクラスにわたって複数のプロパティに対してこれを実行し始めると、多くの繰り返しがあります。

4

2 に答える 2

1

私は次のようなものを提案します:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
using System.Numerics;

namespace Test
{
    class PhaseList
    {
        private Dictionary<int, Complex> mPhases = new Dictionary<int, Complex>();

        public Complex this[int pIndex]
        {
            get
            {
                Complex lRet;
                mPhases.TryGetValue(pIndex, out lRet);
                return lRet;
            }
            set
            {
                mPhases.Remove(pIndex);
                mPhases.Add(pIndex, value);
            }
        }
    }

    class PhasedType
    {
        private PhaseList mPhases = new PhaseList();
        public PhaseList Phases { get { return mPhases; } }
        public static implicit operator Complex(PhasedType pSelf)
        {
            return pSelf.Phases[1];
        }

        public static implicit operator PhasedType(Complex pValue)
        {
            PhasedType lRet = new PhasedType();
            lRet.Phases[1] = pValue;
            return lRet;
        }
    }

    class Bus
    {
        public PhasedType Voltage { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Bus lBus = new Bus();

            lBus.Voltage = new Complex(1.0, 1.0);
            Complex c = lBus.Voltage;
            lBus.Voltage.Phases[1] = c;
            c = lBus.Voltage.Phases[1];
        }
    }
}
于 2013-01-11T00:14:23.773 に答える
1

Can you do something like this? This will work similar to your solution at the bottom but because of the generic class you are not repeating the code for each property.

class Program
{
    static void Main(string[] args)
    {
        Collection<Complex> complex = new Collection<Complex>();
        //TODO: Populate the collection with data

        Complex first = complex.First;
        Complex another = complex.Items[2];
    }
}

public class Complex
{
    // implementation
}


public class Collection<T> where T : class
{
    public List<T> Items { get; set; }

    public T First
    {
        get
        {
            return (Items.Count > 0) ? Items[1] : null;
        }
        set
        {
            if(Items.Count > 0) 
                Items[1] = value;
        }
    }
}
于 2013-01-11T00:03:25.653 に答える