繁体   English   中英

如何为属性定义两种可能的数据类型

[英]How to define two possible data type for property

我正在尝试在 C# 中为 2 个类定义运算符重载: FractionBasicFraction 在外部,它们的行为相同,但在内部, Fraction使用PrimeFactorization (将数字存储为质因数列表),而BasicFraction使用int来存储提名者和分母。

他们会遵循这个接口(如果我使用 JSDoc 类型定义):

public interface IFraction
{
  public (int|Primefactoriztion) a; // nominator
  public (int|Primefactoriztion) b; // denominator
}

但既然我不能这样做,有没有一种简单的方法可以互换使用 Fraction 和 BasicFraction ?
我可以定义所有运算符重载来评估intPrimeFactorization之间的交互。 但是,我不知道如何告诉 C# 接受两者都传递给方法。

Tl; dr:如何使一个属性允许 2 种数据类型?

笔记:

  • 我是高级 C# 的新手(我知道 static 打字和基本的 OOP,但仅此而已)。 我来自 JS 和 Python 背景。
  • 我知道它们可能是具有此功能的好库。 我写这篇文章是为了学习 C#,而不是为了生产。

编辑:目前, PrimeFactorization将因子及其功率存储在Dictionary<int, BasicFraction> (我会使用Fraction ,但它会导致递归;这基本上是BasicFraction的唯一用法)。

你想要一个有区别的联合类型。 C# 本身不支持它们作为一流的语言功能,但您可以使用 OneOf 破解它: https://github.com/mcintyre321/OneOf

所以你最终会得到:

public interface IFraction
{
  public OneOf< int, Primefactoriztion > a; // nominator
  public OneOf< int, Primefactoriztion > b; // denominator
}

但它仍然是一种代码气味。 我认为你需要重新考虑你的整个方法。 此外,使用接口来表示可能不是一个好主意(由于所有堆分配),特别是因为您ab成员似乎是可变的。


一个更好的主意是更改您的Primefactoriztion类型(我希望并假设它是struct ,而不是class )以支持从int的隐式转换和一些成员在操作不安全时显式转换为int (例如,如果值不是整数):

struct Primefactoriztion
{
    public static implicit operator int(Primefactoriztion self)
    {
        return ...
    }
}

这样您就可以安全地消除OneOf<int并仅使用Primefactoriztion

public interface IFraction
{
  public Primefactoriztion a; // nominator
  public Primefactoriztion b; // denominator
}

您想到的称为歧视联合,在 C# 中不能作为语言功能使用。

我将使用一种混合方法,其中分数将包含提名者和分母作为int加上素数列表作为只读属性。 您可以通过两个构造函数使用整数或素数列表初始化分数。 丢失的条目将被延迟计算以最小化计算开销。

将属性设为只读可提高代码的稳健性。 特别是如果您使用的是结构。 请参阅Mutating readonly structs (Eric Lippert 的博客:编码中的精彩冒险)。 但请注意,由于延迟评估,结构仍然是可变的。

public struct Fraction
{
    public Fraction(int nominator, int denominator)
    {
        _nominator = nominator;
        _denominator = denominator;
        _nominatorPrimeFactors = null;
        _denominatorPrimeFactors = null;
    }

    public Fraction(IList<int> nominatorPrimeFactors, IList<int> denominatorPrimeFactors)
    {
        if (nominatorPrimeFactors == null || nominatorPrimeFactors.Count == 0) {
            throw new ArgumentNullException(
                $"{nameof(nominatorPrimeFactors)} must be a non-null, non-empty list");
        }
        if (denominatorPrimeFactors == null || denominatorPrimeFactors.Count == 0) {
            throw new ArgumentNullException(
                $"{nameof(denominatorPrimeFactors)} must be a non-null, non-empty list");
        }
        _nominator = null;
        _denominator = null;
        _nominatorPrimeFactors = nominatorPrimeFactors;
        _denominatorPrimeFactors = denominatorPrimeFactors;
    }

    private int? _nominator;
    public int Nominator
    {
        get {
            if (_nominator == null) {
                _nominator = _nominatorPrimeFactors.Aggregate(1, (x, y) => x * y);
            }
            return _nominator.Value;
        }
    }

    private int? _denominator;
    public int Denominator
    {
        get {
            if (_denominator == null) {
                _denominator = _denominatorPrimeFactors.Aggregate(1, (x, y) => x * y);
            }
            return _denominator.Value;
        }
    }

    private IList<int> _nominatorPrimeFactors;
    public IList<int> NominatorPrimeFactors
    {
        get {
            if (_nominatorPrimeFactors == null) {
                _nominatorPrimeFactors = Factorize(Nominator);
            }
            return _nominatorPrimeFactors;
        }
    }

    private IList<int> _denominatorPrimeFactors;
    public IList<int> DenominatorPrimeFactors
    {
        get {
            if (_denominatorPrimeFactors == null) {
                _denominatorPrimeFactors = Factorize(Denominator);
            }
            return _denominatorPrimeFactors;
        }
    }

    private static List<int> Factorize(int number)
    {
        var result = new List<int>();

        while (number % 2 == 0) {
            result.Add(2);
            number /= 2;
        }

        int factor = 3;
        while (factor * factor <= number) {
            if (number % factor == 0) {
                result.Add(factor);
                number /= factor;
            } else {
                factor += 2;
            }
        }
        if (number > 1) result.Add(number);

        return result;
    }

    public override string ToString()
    {
        if (_nominatorPrimeFactors == null && _denominatorPrimeFactors == null) {
            return $"{_nominator}/{_denominator}";
        }
        string npf = ListToString(_nominatorPrimeFactors);
        string dpf = ListToString(_denominatorPrimeFactors);

        if (_nominator == null && _denominator == null) {
            return $"({npf}) / ({dpf})";
        }
        return $"{_nominator}/{_denominator}, ({npf}) / ({dpf})";


        static string ListToString(IList<int> primeFactors)
        {
            if (primeFactors == null) {
                return null;
            }
            return String.Join(" * ", primeFactors.Select(i => i.ToString()));
        }
    }
}

请注意,声明质数因子列表IList<int>允许您使用int[]List<int>初始化分数。

但值得考虑的是,质因数是否真的需要存储。 在某些计算需要时计算它们还不够吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM