Java中的覆盖方法

时间:2013-03-10 15:18:36

标签: java override

我正在尝试从“Operatii”类中执行一个方法。该方法写在“PolinomIntreg”类中。问题是,当执行时,程序进入覆盖的方法,其中没有代码而不是代码。

我该怎么做才能使程序进入具有代码的程序?

PolinomIntreg类:

import java.util.Scanner;


public class PolinomIntreg<INTEGER> implements IPolinom<INTEGER> {

int grad1, grad2, x, i;
double val = 0;

int[] pol1 = new int[100];
int[] pol2 = new int[100];
int[] pol3 = new int[200];

public void grad(int grad1, int grad2){

    System.out.println("Introduceti gradele celor doua polinoame: ");
    Scanner in = new Scanner(System.in);
    this.grad1 = in.nextInt();
    this.grad2 = in.nextInt();


}


public void coef(int pol1[], int pol2[], int grad1, int grad2){

    System.out.println("Introduceti coeficientii reali ai primului polinom: ");

    for(i = 0; i < this.grad1; i++)
    {
        Scanner in = new Scanner(System.in);
        pol1[i] = in.nextInt();
    }

    System.out.println("Introduceti coeificientii reali ai celui de al doilea polinom: ");

    for(i = 0; i < this.grad2; i++)
    {
        Scanner in = new Scanner(System.in);
        pol2[i] = in.nextInt();
    }
}

public void add(int pol1[], int pol2[], int grad1, int grad2){

    if(this.grad1 > this.grad2)
    {
        for(i = 0; i < this.grad1 ; i++)
            pol1[i] = pol2[i] + pol1[i];
    }
    else
        for(i = 0; i < this.grad2 ; i++)
            pol2[i] = pol2[i] + pol1[i];
}

public void substract(int pol1[], int pol2[], int grad1, int grad2){

    if(this.grad1 > this.grad2)
    {
        for(i = 0; i < this.grad1 ; i++)
            pol1[i] = pol2[i] - pol1[i];
        afisare(pol1,this.grad1);
    }
    else
        for(i = 0; i < this.grad2 ; i++)
            pol2[i] = pol2[i] - pol1[i];
        afisare(pol2,this.grad2);

}

public void value(int pol1[], int grad1){


    System.out.println("Introduceti o valoare pentru x: ");
    Scanner in = new Scanner(System.in);
    x = in.nextInt();

    for( i = 0; i < this.grad1; i++)
        val = val + pol1[i] * Math.pow(x,i);

    System.out.println("Valoarea polinomului in punctul" + x + " este " + val);
}

public void afisare(int pol1[], int grad1){

    for(i = grad1 - 1; i >= 0; i--)
        System.out.println(pol1[i] + "^" + i + "+ ");
}


public void multiply(int pol1[], int pol2[], int grad1, int grad2){

    int j = 0;

    for ( i = 0; i < this.grad1 ; i++)
        for (j = 0; j < this.grad2 ; j++)
        {
            pol3[i+j] = pol3[i+j] + pol1[i] + pol2[j]; 
        }
}


@Override
public void add(INTEGER[] pol1, INTEGER[] pol2, INTEGER grad1, INTEGER grad2) {
    // TODO Auto-generated method stub

}

@Override
public void value(INTEGER[] pol1, INTEGER grad1) {
    // TODO Auto-generated method stub

}


@Override
public void multiply(INTEGER[] pol1, INTEGER[] pol2, INTEGER[] pol3,
        INTEGER grad1, INTEGER grad2, INTEGER grad3) {
    // TODO Auto-generated method stub

}


@Override
public void grad(INTEGER grad1, INTEGER grad2) {


}


@Override
public void coef(INTEGER[] pol1, INTEGER[] pol2, INTEGER grad1,
        INTEGER grad2) {
    // TODO Auto-generated method stub

}


@Override
public void afisare(INTEGER[] pol1, INTEGER grad1) {
    // TODO Auto-generated method stub

}


@Override
public void substract(INTEGER[] pol1, INTEGER[] pol2, INTEGER grad1,
        INTEGER grad2) {
    // TODO Auto-generated method stub

}

}   

Operatii课程:

import java.util.Scanner;


public class Operatii {

static Integer[] pol1 = new Integer[100];
static Integer[] pol2 = new Integer[100];
static Integer[] pol3 = new Integer[200];
static public Integer grad1, grad2,grad3, x, i;

static Float[] pol11 = new Float[100];
static Float[] pol21 = new Float[100];
static Float[] pol31 = new Float[200]; 
    static Float grad11, grad21, grad31;
Float x1;


public static void main(String args[]){

    IPolinom<Integer> poli = new PolinomIntreg<>();
    IPolinom<Float> polr = new PolinomReal<>();

    System.out.println("Apasati 1 pentru operatii cu polinoame cu coef reali");
    System.out.println("Apasati 0 pentru operatii cu polinoame cu coef intregi");

    Scanner in = new Scanner(System.in);
    int meniu = in.nextInt();

    if( meniu == 0){

    poli.grad(grad1, grad2);
    poli.coef(pol1, pol2, grad1, grad2);
    poli.add(pol1, pol2, grad1, grad2);
    //poli.multiply(pol1, pol2, pol3, grad1, grad2, grad3);
    //poli.value(pol1, grad1);
    //poli.afisare(pol1, grad1);
    //poli.afisare(pol2, grad2);

            }

    else if ( meniu == 1 ){
        polr.grad(grad11, grad21);
        polr.coef(pol11, pol21, grad11, grad21);
        polr.add(pol11, pol21, grad11, grad21);
        polr.multiply(pol11, pol21, pol31, grad11, grad21, grad31);
        polr.value(pol11, grad11);
        polr.afisare(pol11, grad11);
        polr.afisare(pol21, grad21);
    }

    else System.out.println("Eroare introducere optiune! ");



}

}

IPolinom是一个界面。

4 个答案:

答案 0 :(得分:1)

如果我正确理解你的问题,如果你试图调用add方法,那么你试图调用最后有'Integer'变量的add方法,所以如果你声明你的grad变量是类型的'int'然后它将转到具有代码的方法。

更确切地说:

    poli.add(pol1, pol2, grad1, grad2);  here grad1 and grad2 are type of Integer. Declare grad1 and grad2 to be int then the add method which has code will be called.

将grad1和grad2声明为

 int grad1, grad2;

更新

还将pol1和pol2的类型更改为int

类型
   static int[] pol1 = new int[100];
   static int[] pol2 = new int[100];

答案 1 :(得分:1)

您没有覆盖方法。要覆盖,您需要使用完全签名(使用int代替Integer)。我建议你将所有代码复制到“无代码的方法”(或自动生成的方法)中,并删除你的非重写方法。

顺便说一下,这是使用@Override 注释强制的最终参数。如果您曾尝试添加它 - 您会看到您的代码未覆盖任何方法。

答案 2 :(得分:1)

从您的代码中,您似乎在签名中使用INTEGER的某些方法,而在int中使用其他方法,例如

public void afisare(int pol1[], int grad1){

}

@Override
public void afisare(INTEGER[] pol1, INTEGER grad1) {
    // TODO Auto-generated method stub

}

您编写的方法具有int签名,Interface的方法具有INTEGER签名。你没有压倒你只是写不同的方法。

将您的实施代码移动到具有正确签名的方法中。

答案 3 :(得分:1)

public void value(int pol1[], int grad1)

public void value(INTEGER[] pol1, INTEGER grad1)

是不同的。您正在调用没有实现的public void value(INTEGER[] pol1, INTEGER grad1)