You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

抽象HyperComplexNumber类运算符+重载实例化问题的最优解决方案咨询

解决抽象超复数类中运算符+的实例化问题

先还原你的代码场景:

public abstract class HyperComplexNumber { 
    public HyperComplexNumber(double real, double imaginary) { 
        Real = real; 
        Imaginary = imaginary; 
    } 
    public double Real; 
    public double Imaginary; 
    public static HyperComplexNumber operator+(HyperComplexNumber left, HyperComplexNumber right) { 
        // 核心问题:抽象类无法直接实例化,此处报错
        return new HyperComplexNumber(left.Real + right.Real, left.Imaginary + right.Imaginary); 
    } 
} 
public class ComplexNumber : HyperComplexNumber { 
    public ComplexNumber(double real, double imaginary) : base(real, imaginary) {}
    // 相关代码 
} 
public class SplitComplexNumber : HyperComplexNumber { 
    public SplitComplexNumber(double real, double imaginary) : base(real, imaginary) {}
    // 相关代码 
} 
public class DualNumber : HyperComplexNumber { 
    public DualNumber(double real, double imaginary) : base(real, imaginary) {}
    // 相关代码 
}

你提到的两个思路确实都有明显弊端:子类重复实现+运算符会产生大量冗余代码,反射又属于不够优雅的“代码坏味道”。这里推荐抽象工厂方法的解决方案,既能避免冗余,又能保证类型安全,完全不需要依赖反射。

具体实现步骤

  1. 在抽象基类中定义一个抽象的创建方法,把实例化的责任交给子类:
public abstract class HyperComplexNumber { 
    public HyperComplexNumber(double real, double imaginary) { 
        Real = real; 
        Imaginary = imaginary; 
    } 
    public double Real; 
    public double Imaginary;

    // 抽象工厂方法:要求所有子类实现,返回自身类型的实例
    protected abstract HyperComplexNumber Create(double real, double imaginary);

    public static HyperComplexNumber operator+(HyperComplexNumber left, HyperComplexNumber right) {
        // 默认假设左右操作数类型一致(若需处理跨类型相加,可额外添加类型判断逻辑)
        return left.Create(left.Real + right.Real, left.Imaginary + right.Imaginary);
    } 
}
  1. 每个子类实现Create方法,返回自身的实例:
public class ComplexNumber : HyperComplexNumber { 
    public ComplexNumber(double real, double imaginary) : base(real, imaginary) {}

    protected override HyperComplexNumber Create(double real, double imaginary) {
        return new ComplexNumber(real, imaginary);
    }

    // 其他相关代码 
} 

public class SplitComplexNumber : HyperComplexNumber { 
    public SplitComplexNumber(double real, double imaginary) : base(real, imaginary) {}

    protected override HyperComplexNumber Create(double real, double imaginary) {
        return new SplitComplexNumber(real, imaginary);
    }

    // 其他相关代码 
} 

public class DualNumber : HyperComplexNumber { 
    public DualNumber(double real, double imaginary) : base(real, imaginary) {}

    protected override HyperComplexNumber Create(double real, double imaginary) {
        return new DualNumber(real, imaginary);
    }

    // 其他相关代码 
}

额外说明

  • 这个方案完全符合开闭原则:加法逻辑只在基类写一次,后续新增超复数子类时,只需要实现Create方法即可继承加法功能,无需重复编写运算符逻辑。
  • 如果需要处理不同类型超复数相加的场景,可以在operator+中添加类型判断,或者定义一套类型转换规则——不过通常同类型超复数相加才是更合理的业务场景。
  • 相比反射,这个方案在编译时就能检查类型错误,性能更优;相比子类重复实现运算符,彻底消除了代码冗余。

内容的提问来源于stack exchange,提问作者AstroRP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:52:39