【问题标题】:Java Interfaces type declarations for methods方法的 Java 接口类型声明
【发布时间】:2019-12-29 16:56:24
【问题描述】:

我有一个包含一些算术运算的接口。我为复数创建了一个类,它将实现这些操作。

package numeric;
public interface Numeric {

    public void add();
    public void subtract();
    public void multiply();
}

我的班级看起来像这样

package numeric;
public class Complex implements Numeric {

    private int Re, Im;
    public void add (Complex x){
        this.Re+=x.Re;
        this.Im+=x.Im;  
    }
}

我的方法目前在接口中没有参数,因为我不知道要制作哪种类型,因为我也必须对分数使用相同的接口。如果我在类中实现它们时输入Complex,则会出现错误,指出该类必须实现所有接口方法。

【问题讨论】:

  • 你可能想看看generics。在本例中,T extends Numeric<T> 似乎很合适。
  • @Turing85 我一定不同意。您可以将整数添加到复数,不是吗?
  • @Bohemian 你仍然可以在add(int real) 上添加广告Complex。但是有些东西我会归类为Numeric,通常不能添加int(例如极坐标形式的复数)。
  • 是否预见到可能会添加不同的类型?例如,一个复数加到一个非复数上?此外,请考虑不变性 - 也就是说,您的方法可以(或应该,如果遵循最佳实践)返回作为操作结果的新实例,而不是改变实例。

标签: java class variables interface


【解决方案1】:

您可以使用的一种方法是强制每个Numeric 子类型相互转换(有点像Number 中定义的intValue、longValue、doubleValue 等方法,尽管目的略有不同)。

interface Numeric {
    public void add(Numeric other);

    public void subtract(Numeric other);

    public void multiply(Numeric other);

    public Complex toComplex();

    public Fraction toFraction();
}

这有优缺点,其中一个缺点是你的接口现在必须知道它的所有实现(不确定这叫什么,密封类型?)

上面可以这样实现:

class Complex implements Numeric {

    private int Re, Im;

    @Override
    public void add(Numeric other) {
        Complex complex = other.toComplex();
        this.Re += complex.Re;
        this.Im += complex.Im;
    }

    @Override
    public void subtract(Numeric other) {
        Complex complex = other.toComplex();
        this.Re -= complex.Re;
        this.Im -= complex.Im;
    }

    @Override
    public Complex toComplex() {
        return this;
    }

    @Override
    public Fraction toFraction() {
        //convert to fraction
        return null;
    }
    //rest of the implementation
}

附带说明:通常,此类 API 鼓励数据结构的不变性,因此建议将其更改为:

interface Numeric {
    public Numeric add(Numeric other);

    public Numeric subtract(Numeric other);

    public Numeric multiply(Numeric other);

    public Complex toComplex();

    public Fraction toFraction();
}

和

class Complex implements Numeric {

    private final int re, im;

    public Complex(int re, int im) {
        this.re = re;
        this.im = im;
    }

    @Override
    public Numeric add(Numeric other) {
        Complex complex = other.toComplex();
        return new Complex(this.re + complex.re, this.im + complex.im);
    }

    @Override
    public Numeric subtract(Numeric other) {
        Complex complex = other.toComplex();
        return new Complex(this.re - complex.re, this.im - complex.im);
    }

    @Override
    public Complex toComplex() {
        return this;
    }

    @Override
    public Fraction toFraction() {
        //convert to fraction
        return null;
    }

    //rest of the implementation
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2014-02-02
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多