简体   繁体   English

Java中的重写方法

[英]Override method in Java

I am trying to execute a method from the class "Operatii". 我正在尝试从“ Operatii”类中执行一种方法。 The method is written in class "PolinomIntreg". 该方法写在“ PolinomIntreg”类中。 The problem is that when executed, the program goes to the overrided method where there is no code and not the one where is code. 问题在于,在执行时,程序将转到没有代码的覆盖方法,而不是没有代码的方法。

What can I do so the program goes to the one that has code? 我该怎么办才能使程序转到有代码的程序?

PolinomIntreg class: 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 class: 操作课:

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 is an interface. IPolinom是一个接口。

If I understand your question correctly, If you are trying to call the add method then you are trying to call the add method which has variables of 'Integer' at the end, so if you declare your grad variables to be of type 'int' then it will go to the method that has code. 如果我正确理解了您的问题,如果您尝试调用add方法,那么您将尝试调用在末尾具有'Integer'变量的add方法,因此,如果您将grad变量声明为'int'类型然后它将转到具有代码的方法。

So to be more precise: 因此更准确地说:

    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.

declare grad1 and grad2 like 像这样声明grad1和grad2

 int grad1, grad2;

Update: 更新:

Also change type of pol1 and pol2 to be of type int 还要将pol1和pol2的类型更改为int类型

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

You are not overriding the methods . 您没有覆盖方法 In order to override, you need to use the exact signature ( you use int instead of Integer ). 为了覆盖,您需要使用精确的签名(使用int而不是Integer )。 I suggest you copy all your code inside the "methods without code" (or auto-generated methods) and remove your non overriding methods. 我建议您将所有代码复制到“没有代码的方法”(或自动生成的方法)中,并删除非覆盖方法。

BTW, this is the ultimate argument for force using @Override annotation. 顺便说一句,这是使用@Override注释强制使用最终参数 If you had tried to add it - you would see your code isn't override any method. 如果您尝试添加它-您会看到您的代码没有覆盖任何方法。

From your code you seem to have some methods with INTEGER in the signature and others with int , for example 从您的代码中,您似乎在签名中有一些使用INTEGER方法,而其他一些具有int ,例如

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

}

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

}

The methods you wrote have the int signature and the methods of the Interface have the INTEGER signature. 您编写的方法具有int签名,而Interface的方法具有INTEGER签名。 You are not overriding the methods you are simply writing different ones. 您不会覆盖您只是在编写不同方法的方法。

Move your implementation code into the methods with the correct signature. 用正确的签名将实现代码移到方法中。

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

and

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

are different . 是不同的 。 You are calling public void value(INTEGER[] pol1, INTEGER grad1) which has no implementation . 您正在调用没有实现的public void value(INTEGER[] pol1, INTEGER grad1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM