类型“subclassName”的方法未定义

问题描述 投票:0回答:1

我在同一个包中有三个名为 Matrix、BasicOperations 和 Decisionant 的类。

我想在单独的类中定义单独的方法(例如,与在 Decisionant 类下查找行列式相关的方法),但我在第 7 行和行列式类中的 9。 我不知道我在哪里犯了错误,所以我不得不问,我怎样才能让这些方法发挥作用?

Matrix.java(用于创建对象)

package Matrix;

import java.util.ArrayList;

public class Matrix {

    public int n;
    public int m;
    public ArrayList<Double> Matrices = new ArrayList<Double>();
    
    public Matrix(int n, int m) {
        
        this.n = n;
        this.m = m;
        
        Matrices = new ArrayList<Double>(n*m);
        
    }
}

BasicOperations.java(包含基本操作方法)

package Matrix;
public class BasicOperations extends Matrix{

    public BasicOperations(int n, int m){
        super(n, m);
    }
    
    public void putTo(int a, int b, double value) {
        
        //allows user to put a value at coordinate ab in the matrix.
        int index = (a-1)*m + b - 1;
        Matrices.add(index, value);
        
    }

    public void changeValue(int a, int b, double newValue) {
        
        //allows user to replace the value at coordinate ab in the matrix with a new value.
        int index = (a-1)*m + b - 1;
        Matrices.remove(index);
        Matrices.add(index, newValue);
        
    }

    public void printValue(int a, int b) {
        
        //allows user to print the value at coordinate ab in the matrix.
        int index = (a-1)*m + b - 1;
        double element = Matrices.get(index);
        System.out.println("Bu matrisin "+a+". satır "+b+". sütunda bulunan elemanı "+element+" sayısıdır.");
        System.out.print("\n");
        
    }

    public double getValue(int a, int b) {
        
        //allows user to get the value at coordinate ab in the matrix.
        int index = (a-1)*m + b - 1;
        double value = Matrices.get(index);
        return value;
        
    }

    public void printMatrix() {
        
        //allows user to print the matrix.
        for(int a = 1; a <= n; a++) {
            System.out.print("[");
            for(int b = 1; b <= m; b++) {
                if(b == 1) {
                    System.out.print(Matrices.get((a-1)*m + b - 1).toString());
                } else {
                    System.out.print("  " +Matrices.get((a-1)*m + b - 1).toString());
                }
            }
            System.out.println("]");
        }
        System.out.print("\n");
        
    }

    public int getRowNumber() {
        
        //allows user to get number of rows.
        return n;
        
    }
    
    public int getColumnNumber() {
        
        //allows user to get number of columns.
        return m;
        
    }

}

Determinant.java(用于包含行列式运算方法)

package Matrix;

class Determinant{
    
    public double get2x2Determinant() {
        
        if(this.getColumnNumber() * this.getRowNumber() == 4) {
            
            double det = (this.getValue(1, 1) * this.getValue(2, 2)) - (this.getValue(1, 2) * this.getValue(2, 1));
            return det;
            
        } else {
            
            return 0;
            
        }
        
    }
java class methods typeerror
1个回答
0
投票

您的直接问题源于尝试调用该类不存在的方法,

getValue(...)
。是的,该方法存在于 Matrix 类以及从它扩展的所有类中,并且被声明为
public
,因此可以在作为 Matrix 或 Matrix 子级的任何对象上调用该方法,但决定因素不是这样的类不是从 Matrix 类派生的,它除了 object 之外没有父类。因此,当您调用
this.getValue(...)
时,编译器会查看 Decisionant 类及其父类 Object 的内部,发现该方法不存在,并调用“foul”。

一个快速、简单且错误的解决方案是从 Matrix 或 BasicOperations 类扩展确定性,然后是的,它会有这个方法,但随后你就会陷入糟糕的代码设计,一个过于碎片化的类。 Matrix 类应该真正保存数学矩阵的所有状态和行为,并且您无缘无故地将其分开。这只会增加混乱、错误,并且不会给你带来任何好处。相反,创建一个完整且完整的 Matrix 类。除掉不属于矩阵的东西,例如:

public ArrayList<Double> Matrices = new ArrayList<Double>();
。这并不真正属于 Matrix 内部。

© www.soinside.com 2019 - 2024. All rights reserved.