当前位置: 首页 > 知识库问答 >
问题:

Java中用复杂类声明泛型变量

梁丘远航
2023-03-14

我有一个类用复数来计算,一个实部和一个虚部作为双类型。在其他部分,我有一个rational类来计算有理数。现在我想让我的复形类能在实部和虚部是有理数的情况下运算。我读过一些关于泛型的文档,但我不知道如何将实部和虚部声明为泛型类型,以及当实部和虚部是双倍或有理时如何使用添加2复数的方法。这是我的测试代码:

import java.util.regex.Pattern;

public class Complex {
    private double real;
    private double imaginary;
    private Rational qreal;
    private Rational qimaginary;

    public Complex(double real, double imaginary) {
        super();
        this.real = real;
        this.imaginary = imaginary;
    }
    public Complex(Rational real, Rational imaginary) {
        this.qreal = real;
        this.qimaginary = imaginary;
    }
    public Complex(String z) {
        z = z.replaceAll(" ","");
        if(z.contains("i") || z.contains("j")){
            if(z.contains("+")) {
                String[] z1 = z.split(Pattern.quote("+"));
                this.real = Double.parseDouble(z1[0]);
                this.imaginary = Double.parseDouble(z1[1].substring(0, z1.length-1));
            }
            else if(z.contains("-")) {
                String[] z1 = z.split(Pattern.quote("-"));
                this.real = Double.parseDouble(z1[0]);
                this.imaginary = -Double.parseDouble(z1[1].substring(0, z1.length-1));
            }
            else System.out.println("Syntax Error");
        }
        else System.out.println("The complex must only contains i or j as imaginary unit");
    }
    public double getReal() {
        return real;
    }
    public void setReal(double real) {
        this.real = real;
    }
    public double getImaginary() {
        return imaginary;
    }
    public Rational getQreal() {
        return qreal;
    }
    public void setQreal(Rational qreal) {
        this.qreal = qreal;
    }
    public Rational getQimaginary() {
        return qimaginary;
    }
    public void setQimaginary(Rational qimaginary) {
        this.qimaginary = qimaginary;
    }
    public void setImaginary(double imaginary) {
        this.imaginary = imaginary;
    }

    Complex opposite(Complex z) {return new Complex(-z.real, -z.imaginary);}
    double abs() {return Math.hypot(this.real, this.imaginary);}
    Complex conjugate() {return new Complex(real, -imaginary);}
    Complex inverse() {
        if(this.real == 0 && this.imaginary == 0) return new Complex(Double.NaN, Double.NaN);
        else {
            Complex c = this.conjugate();
            double abs_square = Math.pow(this.abs(), 2.);
            return new Complex(c.real / abs_square, c.imaginary / abs_square);
        }
    }

    Complex add2(Complex z) {
        System.out.println("Suma " + this.qreal.add(z.qreal) + " " + this.qimaginary.add(z.qimaginary) + "i");
        return new Complex(this.qreal.add(z.qreal), this.qimaginary.add(z.qimaginary));
    }
    Complex add(Complex z) {return new Complex(this.real + z.real, this.imaginary + z.imaginary);}
    Complex subtract(Complex z) {return add(z.opposite(z));}
    Complex product(Complex z) {
        double r, i;
        r = this.real * z.real - this.imaginary * z.imaginary;
        i = this.real * z.imaginary + this.imaginary * z.real;
        return new Complex(r, i);
    }
    Complex div(Complex z) {
        Complex num = this.product(z.conjugate());
        double den = Math.pow(Math.hypot(z.real, z.imaginary), 2.);
        return new Complex(num.real / den, num.imaginary / den);
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Complex [real=" + real + ", imaginary=" + imaginary + ", qreal=" + qreal + ", qimaginary=" + qimaginary
                + "]";
    }


    /*@Override
    public String toString() {
        if(imaginary > 0.) {
            if (imaginary == 1.)
                return real + " + " + "i";
            return real + " + " + imaginary + "i";
        }
        else if(imaginary < 0.) {
            if (imaginary == -1.)
                return real + " - " + "i";
            return real + " " + imaginary + "i";
        }
        else if(imaginary == 0.)
            return "" +  real;
        else if(real == 0.)
            return  imaginary + "i";
        else
            return "0";
    }*/



}

如果您看到了我实现了2个add方法的代码,但我只想要一个,因此对于其他方法,也需要toString()。

共有1个答案

薛钧
2023-03-14

当您想要保留类型信息时,泛型很有用。但是泛型类型仍然需要有一些已知的接口才能使用它。由于doublerational不共享公共接口,因此不可能直接创建一个适用于这两种类型的通用实现。

您可以做的是创建一个具有两个实现的接口complexrationalcomplex:

public interface Complex<T> {

    T getReal();
    T getImaginary();

    Complex<T> opposite(Complex<T> z);
    double abs();
    Complex<T> conjugate();
    Complex<T> inverse();

    Complex<T> add(Complex<T> z);
    Complex<T> subtract(Complex<T> z);
    Complex<T> product(Complex<T> z);
    Complex<T> div(Complex<T> z);

}

public class DoubleComplex implements Complex<Double> {

    private final double real;
    private final double imaginary;

    ...

    @Override
    public Complex<Double> add(Complex<Double> z) {
        return new DoubleComplex(this.real + z.getReal(), this.imaginary + z.getImaginary());
    }

    ...
}

public class RationalComplex implements Complex<Rational> {

    private final Rational real;
    private final Rational imaginary;

    ...

    @Override
    public Complex<Rational> add(Complex<Rational> z) {
        return new RationalComplex(this.real.add(z.getReal()), this.imaginary.add(z.getImaginary()));
    }

    ...
}
 类似资料:
  • 问题是如何使用对象类型发送Header,而不是HTTPClient声明中提供的HttpHeaders。 我在VS代码中得到的错误信息如下所示 “{headers:GetLoggedInUserHeaderRequestParam;}”类型的[ts]参数不可分配给“{headers?:HttpHeaders{[Header:string]:string string[];};};”类型“的参数;”观

  • 9. 不完全类型和复杂声明 在第 1 节 “复合类型与结构体”讲过算术类型、标量类型的概念,现在又学习了几种类型,我们完整地总结一下C语言的类型。下图出自[Standard C]。 图 23.5. C语言类型总结 C语言的类型分为函数类型、对象类型和不完全类型三大类。对象类型又分为标量类型和非标量类型。指针类型属于标量类型,因此也可以做逻辑与、或、非运算的操作数和if、for、while的控制表达

  • 问题内容: 我有一个其键为通用类型,而值为类型。如果键是的实例,则值必须是,并且相同的规则适用于任何其他键值对。我尝试了以下方法,但无法编译: 目前,我必须使用“部分”泛型进行声明: 我知道这很糟糕,但是我目前没有更好的选择。在这种情况下可以使用泛型吗? 更新 也许我没有明确表达我的问题。我想要一张能够: 并且以下代码不应编译: 键和值应始终具有相同的泛型类型,而泛型类型可以是任意泛型,并且显然扩

  • 当我试图运行我的BMI计算器时,我经常遇到这样一个错误“Cannot resolve method'println(java.lang.String,double)”当我试图运行我的BMI计算器时,我如何为一个double类型的变量声明一条语句?请帮帮我。这是我代码的链接

  • “绑定不匹配:Team类型不是league类型 的有界参数 >的有效替代品。”

  • 当我试图运行我的BMI计算器时,我经常遇到这样一个错误“Cannot resolve method'println(java.lang.String,double)”当我试图运行我的BMI计算器时,我如何为一个double类型的变量声明一条语句?请帮帮我。这是我代码的链接