【问题标题】:how to have multiply constructors in one class? [closed]如何在一个类中有多个构造函数? [关闭]
【发布时间】:2020-03-26 11:31:57
【问题描述】:

我想在一个类中有两个构造函数,但我不能。我知道这是可能的,但我找不到我的错误。

     public static class Matrix{
        int [][] matrix;
        int row;
        int column;
        String matrixName;
        Matrix (String [] input, String name) {
            matrixName = name;
            column = input.length;
            row = input[0].split(",").length;
            matrix = new int [row][column];
            initialize(input);
        }

        Matrix (Matrix A, char ch) {
            if (ch == 'T'){
                column = A.row;
                row = A.column;
            }
            else{
                column = A.column;
                row = A.row;
            }
            matrix = new int[row][column];
            matrixName = "result";
        }

第二个矩阵不能定义为构造函数。

【问题讨论】:

  • 您遇到了什么错误?
  • @AbhiN 在另一个类中我有 Matrix result = new Matrix (Matrix A, 'R'); 并且我收到此错误 Error:(121, 47) java: ')' expected Error:(121, 48) java: not a statement 错误: (121, 49) java: ';'预计
  • 只写A作为new调用中的第一个参数,而不是Matix A。
  • 我认为您不能将 Matrix 作为参数传递,因为如果允许,那么对于该 Matrix A 对象的初始化,将需要执行第二个构造函数,您可以使用第二个构造函数来执行它并且在该执行中,第二个构造函数也需要再次执行,这样你就会陷入无限循环。
  • 投反对票,因为您没有分享错误消息。见minimal reproducible example,谢谢。

标签: java class constructor


【解决方案1】:

第二个构造函数没有问题。问题在于声明的第一行public static class Matrix。请注意,您不能在此处使用 static 修饰符。仅允许使用 public、abstract 和 final。

修正后,您可以按如下方式测试您的课程:

class Matrix {
    int[][] matrix;
    int row;
    int column;
    String matrixName;

    Matrix(String[] input, String name) {
        matrixName = name;
        column = input.length;
        row = input[0].split(",").length;
        matrix = new int[row][column];
        // initialize(input);
        System.out.println("First");
    }

    Matrix(Matrix A, char ch) {
        if (ch == 'T') {
            column = A.row;
            row = A.column;
        } else {
            column = A.column;
            row = A.row;
        }
        matrix = new int[row][column];
        matrixName = "result";
        System.out.println("Second");
    }
}

public class Main {
    public static void main(String[] args) {
        Matrix m1 = new Matrix(new String[] { "a", "b" }, "Hello");
        Matrix m2 = new Matrix(m1, 'X');
    }
}

输出:

First
Second

【讨论】:

  • arvind 先生,您认为我对 OP 帖子的评论有意义吗,我只是一个想法,您能告诉我吗
  • @AbhinavChauhan - 不,您的理解不正确。检查示例输出。
  • 你能通过 Matrix m2 = new Matrix(m2,'x'); 来检查吗,我不确定我的假设
  • @AbhinavChauhan - 你的提议,can you check by doing Matrix m2 = new Matrix(m2,'x'); 在语法上是错误的。它将因编译错误而失败。
【解决方案2】:

它是嵌套在另一个类中的类吗?因为类只有在像这样嵌套在另一个类中时才能是静态的:

public class Matrix {

    public static class NestedClass {

        int number;
        String string;

        public NestedClass(NestedClass A, String string) {
            this.number = A.number;
            this.string = string;
        }

        public NestedClass(NestedClass A) {
            this.string = A.string;
        }
    }
}

类矩阵不能是静态的,只能是最终的或抽象的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2015-06-20
    • 2016-01-31
    • 1970-01-01
    相关资源
    最近更新 更多