Java 引用子类中的数组

Java 引用子类中的数组,java,Java,所以我在写一个程序来加和减多项式。多项式以字符串形式出现(例如:4x^7-2x^5+3x^2+78)并将其拆分为项(例如4x^7),然后将系数值指定给多项式阵列[指数] 这是我任务的第一部分,因此我有一个界面,如下所示: public interface PolynomialInterface { PolynomialInterface add(PolynomialInterface other); // Effect: Adds value to owner of addPolynomial

所以我在写一个程序来加和减多项式。多项式以字符串形式出现(例如:4x^7-2x^5+3x^2+78)并将其拆分为项(例如4x^7),然后将系数值指定给多项式阵列[指数]

这是我任务的第一部分,因此我有一个界面,如下所示:

public interface PolynomialInterface {
PolynomialInterface add(PolynomialInterface other);

// Effect: Adds value to owner of addPolynomial method.
// Postcondition: Return value = this + value.
PolynomialInterface subtract(PolynomialInterface other);

// Effect: Subtracts value from owner of addPolynomial method.
// Postcondition: Return value = this - value.
void readPolynomial();

// Postcondition: polynomial read.
String toString();
// Postcondition: polynomial converted to string.
}
以下是我目前的代码:

import java.lang.*;

public class ArrayWithExponentAsIndexPolynomial implements     PolynomialInterface {
Integer PolynomialArray[] = new Integer[1000];
CharSequence minus = "-";
CharSequence plusMinus = "+-";
boolean FirstElementPos = true;

public ArrayWithExponentAsIndexPolynomial(String input) {
    if (input.charAt(0) == '-') {
        input = input.substring(1);
        FirstElementPos = false;
    }
    String inputPolynomial = input.replaceAll("-", "+-");
    // input.replace(minus, plusMinus);
    System.out.println(inputPolynomial);
    String[] splitTerms = inputPolynomial.split("\\+");
    // int PolynomialArray[] = new int[100];

    for (int i = 0; i <= splitTerms.length - 1; i++) {
        System.out.println(splitTerms[i]);
    }
    String tempTemp = splitTerms[1];

    int coef;
    int exponent;
    String tempExp = null;
    for (int i = 0; i < splitTerms.length; i++) {

        String tempTerm = splitTerms[i];
        System.out.println();
        System.out.println("Term we are working with " + tempTerm);
        boolean tempPos = true;
        if (tempTerm.contains("-")) {
            tempTerm = tempTerm.substring(1);
            System.out.println("After removing negative from term: "
                    + tempTerm);
            tempPos = false;
        }
        int IndexOfexponent = tempTerm.indexOf('^');

        if (IndexOfexponent == -1) {
            exponent = 1;
            // FirstElementPos = true;
        } else {
            tempExp = tempTerm.substring(IndexOfexponent + 1);
            exponent = Integer.parseInt(tempExp);

        }
        System.out.println("The exp is " + exponent);
        // String tempTerm = splitTerms[i];
        System.out.println("The term rn is: " + tempTerm);
        String tempTermNoCarrot = tempTerm.replaceAll("\\^" + tempExp, "");
        String tempCoef = tempTermNoCarrot.replaceAll("x", "");
        // String tempCoef = tempTermNoX.replaceAll(tempExp, "");
        System.out.println("THe Coeff rn is: " + tempCoef);
        coef = Integer.parseInt(tempCoef);

        if (tempPos == false || FirstElementPos == false) {
            coef = (coef * -1);
        }

        System.out.println("After everything, Coef is:" + coef
                + " and exp is: " + exponent);
        PolynomialArray[exponent] = coef;

    }

}

public PolynomialInterface add(PolynomialInterface other) {
    String finalOutput=null;
    //Integer top = this.PolynomialArray[i];
    Integer Sum[] = new Integer[100];
    for (int i = 99; i >= 1; i--){
        Integer top = this.PolynomialArray[i];
        Integer bottom = other.PolynomialArray[i];
        Sum[i] = top + bottom;
    }





    String tempOutput = null;
    for (int i = 99; i >= 1; i--) {

        if (Sum[i] != null && Sum[i] != 0) {
            tempOutput += "+";
            int outputCoef = Sum[i];
            tempOutput += outputCoef;
            tempOutput += "x^";
            tempOutput += i;
        }
    }
    String RemoveNull = tempOutput;
    tempOutput = RemoveNull.replaceAll("null", "");
    if (tempOutput.charAt(0) == '+') {
        tempOutput = tempOutput.substring(1);
    }
    tempOutput = tempOutput.replaceAll("\\+-","-");
    finalOutput = tempOutput;




    return new ArrayWithExponentAsIndexPolynomial(finalOutput);



}

public PolynomialInterface subtract(PolynomialInterface other) {
    return other;
}

public void readPolynomial() {

}

public String toString() {
    String output = null;
    for (int i = 99; i >= 1; i--) {

        if (PolynomialArray[i] != null && PolynomialArray[i] != 0) {
            output += "+";
            int outputCoef = PolynomialArray[i];
            output += outputCoef;
            output += "x^";
            output += i;
        }
    }
    String outputTemp = output;
    output = outputTemp.replaceAll("null", "");
    if (output.charAt(0) == '+') {
        output = output.substring(1);
    }
    output = output.replaceAll("\\+-","-");
    return output;
}

}
import java.lang.*;
带有指数的公共类数组为多项式接口{
整数多项式阵列[]=新整数[1000];
CharSequence减号=“-”;
字符序列plusMinus=“+-”;
布尔值FirstElementPos=true;
带指数的公共数组为单项式(字符串输入){
如果(输入字符(0)='-'){
输入=输入。子字符串(1);
FirstElementPos=false;
}
字符串InputPolymonal=input.replaceAll(“-”,“+-”);
//输入。替换(减,加);
系统输出打印LN(输入多项式);
String[]splitTerms=inputPolymonal.split(\\+);
//整数多项式阵列[]=新整数[100];
对于(int i=0;i=1;i--){
整数top=this.PolynomialArray[i];
整数底=其他多项式阵列[i];
总和[i]=顶部+底部;
}
字符串tempOutput=null;
对于(int i=99;i>=1;i--){
if(Sum[i]!=null&&Sum[i]!=0){
tempOutput+=“+”;
int outputCoef=总和[i];
tempOutput+=输出系数;
tempOutput+=“x^”;
tempOutput+=i;
}
}
String RemoveNull=tempOutput;
tempOutput=RemoveNull.replaceAll(“null”,为“”);
if(tempOutput.charAt(0)='+'){
tempOutput=tempOutput.substring(1);
}
tempOutput=tempOutput.replaceAll(“\\+-”,“-”);
最终输出=临时输出;
返回新数组,其指数为非多项式(最终输出);
}
公共多项式接口减法(多项式接口其他){
归还他人;
}
公共图书馆{
}
公共字符串toString(){
字符串输出=null;
对于(int i=99;i>=1;i--){
if(PolynomialArray[i]!=null&&PolynomialArray[i]!=0){
输出+=“+”;
int outputCoef=多项式阵列[i];
输出+=输出系数;
输出+=“x^”;
输出+=i;
}
}
字符串输出EMP=输出;
output=outputTemp.replaceAll(“空”,为“”);
if(输出字符(0)='+'){
输出=输出。子字符串(1);
}
output=output.replaceAll(“\\+-”,“-”);
返回输出;
}
}
我的问题是在加法中,如何引用“其他”对象中的多项式阵列。当我做其他的时候,多项式阵列[i]它说多项式阵列不能被解析,或者在接口中不是一个字段意义,不存在这样的东西。我想知道有没有一种方法可以在不改变界面的情况下引用我的预期目标,因为在我未来的项目中,我需要使用它

对不起,我不清楚。这是我第一次发帖:)


*快速编辑。我还没有完成我的代码,所以这里和那里有一些占位符和一些随机打印语句

整数多项式数组[]=newinteger[1000]


这是您的实现类
arrayWithExponentasindepolymonal
添加的内容。接口合同中没有指定它。您正试图从接口引用中获取
多项式阵列[]
。那不行。您需要像
((ArrayWithExponentAsIndexPolynomial)other)一样强制转换它

简单地说,
多项式数组
字段是在
数组中定义的,它的指数为多项式
类,且给定的
多项式接口
未知。实际上,接口中只定义方法,不定义字段(如Amit.rk3所述,允许使用静态final字段,但不能解决您的问题)。访问仅由接口标识的对象字段的一种方法是在接口中定义
getSomeField()
方法

我不确定您的赋值是否允许您将
getPolynomialArray()
添加到给定的接口,但这将是最简单的解决方案


否则,尽管不够优雅,但您可以将给定对象强制转换为类并直接访问字段。

事实上,接口中只能定义方法,不能定义字段——这是不正确的。接口可以有字段。只是它们必须是
公共静态final
。请参阅.@Amit.rk3谢谢,我不知道,我将重新表述我的答案。虽然我不认为我会使用它,而且它显然对OP毫无帮助。啊,太完美了!这正是我所缺少的。经过一点研究,我现在完全明白了。谢谢