[英]Override method in Java
我正在嘗試從“ 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
}
}
操作課:
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是一個接口。
如果我正確理解了您的問題,如果您嘗試調用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];
您沒有覆蓋方法 。 為了覆蓋,您需要使用精確的簽名(使用int
而不是Integer
)。 我建議您將所有代碼復制到“沒有代碼的方法”(或自動生成的方法)中,並刪除非覆蓋方法。
順便說一句,這是使用@Override
注釋強制使用的最終參數 。 如果您嘗試添加它-您會看到您的代碼沒有覆蓋任何方法。
從您的代碼中,您似乎在簽名中有一些使用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
簽名。 您不會覆蓋您只是在編寫不同方法的方法。
用正確的簽名將實現代碼移到方法中。
public void value(int pol1[], int grad1)
和
public void value(INTEGER[] pol1, INTEGER grad1)
是不同的 。 您正在調用沒有實現的public void value(INTEGER[] pol1, INTEGER grad1)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.