【问题标题】:Why doesn't the second constructor take in a first constructor's object as input?为什么第二个构造函数不接受第一个构造函数的对象作为输入?
【发布时间】:2021-12-04 16:06:09
【问题描述】:

问题:

为什么第二个构造函数不接受第一个构造函数的对象作为输入?

在代码的更一般概述中,如果你们发现任何有问题的逻辑问题,如果您能指出它们,我将非常感激。

import java.util.Scanner;

    public class Square3x3{
        private static final int DEF_VALUE = -1;
        private static final int ROW_COUNT = 3; 
        private static final int COL_COUNT = 3;
        private static final int LOWER_BOUND = 0;
        
        private int [][] _square;
        
        public Square3x3(){
        _square = new int [ROW_COUNT][COL_COUNT];
            for(int i = 0 ; i < ROW_COUNT ; i++)
            {
                for (int j = 0; j < COL_COUNT; j++)
            {
                _square[i][j] = DEF_VALUE;
                System.out.print(_square[i][j]);
            }
            System.out.println();
        }
        }
        public Square3x3(int[][]array){
            int rows = array.length;
            int column;
            int colCount = 0;
            
            for(int j = 0; j < rows; j++){
                    colCount = j;
            }
            column = colCount;
            if (rows > ROW_COUNT || column > COL_COUNT){
                for (int k = 0; k < ROW_COUNT; k++)
                    for (int g = 0 ; g < COL_COUNT; g++){
                        _square[k][g] = array[k][g];
                    }
            }
            if (rows < 3 || column < 3){
                for (int x = 0; x > array.length; x++)
                    for (int y = 0 ; y > array.length; y++){
                         array[x][y] = -1;
                        }
                        
                    }
            if (rows == 3 && column == 3){
                for (int i = 0; i < array.length; i++){
                    for (int j = 0; j < array.length; j++){
                        _square[i][j] = array[i][j];
                    }
                    }
                }
                
            }    
        public Square3x3(Square3x3 other){
            if (other != null){
                other = new Square3x3();
                for (int i = 0; i < ROW_COUNT; i++)
                    for (int j = 0; j < COL_COUNT; j++){
                        this._square[i][j]=other._square[i][j];
                    }
            }
        }  

【问题讨论】:

  • other = new Square3x3(); 你正在重新初始化对象,值被重置并且你正在复制 reset(-1) 值
  • 不清楚你在问什么。构造函数必须带有new 及其适当的参数。构造函数本身不会调用另一个构造函数(除非您需要同一对象的另一个实例,否则这样做是没有意义的)。你的意思是让构造函数调用非 ctor 方法吗?
  • Square3x3() 输出一个元素为 -1 的 3x3 矩阵。如果我创建这种类型的对象,如何将其输入到 Square3x3(int[][]array)?
  • 为此,您需要一个用于 _square 属性的 getter 方法,以便您可以访问它。但是现在我对您的实际问题更加困惑。
  • 您对 Square3x3(Square 3x3 other)构造函数很满意。至于上面的问题,它们是无关的。

标签: java constructor


【解决方案1】:

不太确定我是否理解您的问题,但在我看来您的构造函数 public Square3x3(Square3x3 other) 有问题。在复制其内容之前,您正在将other 对象重置为other = new Square3x3(); 行中的全新Square3x3。您想要的是以下内容:

public Square3x3(Square3x3 other) {
    this();
    if (other != null) {
        for (int i = 0; i < ROW_COUNT; i++) {
            for (int j = 0; j < COL_COUNT; j++) {
                this._square[i][j] = other._square[i][j];
            }
        }
    }
}

或者,如果您想使用System.arraycopy() 使其更简单:

public Square3x3(Square3x3 other) {
    this();
    if (other != null) {
        for (int i = 0; i < ROW_COUNT; i++) {
            System.arraycopy(other._square[i], 0, this._square[i], 0, COL_COUNT);
        }
    }
}

我仍然对您的问题真正想要实现的目标感到困惑,但是如果您实例化一个 Square3x3 对象,然后出于某种原因您想通过调用 public Square3x3(int[][] array) 构造函数来实例化一个新对象,那么您需要公开一个 getter 以访问 _square 属性内容(考虑将其重命名为 square):

public class Square3x3 {
    (...)

    public int[][] get_square() {
        return _square;
    }

    (...)
}

然后您可以执行以下操作:

public class Main {
    public static void main(String[] args) {
        Square3x3 square = new Square3x3();
        Square3x3 newSquare = new Square3x3(square.get_square());
    }
}

【讨论】:

  • 不应该删除other = new Square3x3(); 吗?
  • 是的,我写了它,但忘记从代码中删除它。感谢@sittsering 的提示!
【解决方案2】:

几周前有一个similar question关于实现第二个构造函数,但这里似乎添加了有关默认构造函数和复制构造函数的更多细节,因此应该修改解决方案。

为什么第二个构造函数不接受第一个构造函数的对象作为输入?

因为第二个构造函数的输入数组需要复制到字段square3x3。所以第二个构造函数应该调用默认的this(),然后按照指定复制输入数组的内容。

值得注意的是,通常无参数构造函数调用带有参数的构造函数并提供默认值,但绝对不是这种情况,因为这会导致在这两个构造函数中重复创建数组.

因此,第一个(默认)构造函数创建并填充数组字段。 第二个构造函数处理从输入数组复制数据。复制(第三个)构造函数调用第二个:

public class Square3x3 {
    private int[][] square3x3;

    public Square3x3() {
        this.square3x3 = new int[][] {
           {-1, -1, -1},
           {-1, -1, -1},
           {-1, -1, -1}
        };
    }

    public Square3x3(int[][] array) {
        this();
        for (int i = 0, n = Math.min(3, array == null ? -1 : array.length); i < n; i++) {
            for(int j = 0, m = Math.min(3, array[i] == null ? -1 : array[i].length); j < m; j++) {
                square3x3[i][j] = array[i][j];
            }
        }
    }

    public Square3x3(Square3x3 other) {
        this(null == other ? null : other.square3x3);
    }

    public int[][] getSquare() {
        return square3x3;
    }
}

此外,初始化可以从默认构造函数中移出,然后它可以为空,第二个构造函数不需要调用它。这样节省了几行代码,但并不能完全满足指定的要求。

public class Square3x3 {
    private int[][] square3x3 = {
       {-1, -1, -1},
       {-1, -1, -1},
       {-1, -1, -1}
    };

    public Square3x3() {
        // empty, provided just for invocation
    }

    public Square3x3(int[][] array) {
        for (int i = 0, n = Math.min(3, array == null ? -1 : array.length); i < n; i++) {
            for(int j = 0, m = Math.min(3, array[i] == null ? -1 : array[i].length); j < m; j++) {
                square3x3[i][j] = array[i][j];
            }
        }
    }

    public Square3x3(Square3x3 other) {
        this(null == other ? null : other.square3x3);
    }
// ...
}

测试:

Square3x3 def = new Square3x3();
Square3x3 copy = new Square3x3((Square3x3) null); // casting needed to resolve ambiguity
Square3x3 arr = new Square3x3(new int[][]{{1, 2, 3, 4}, null, {-10}, {5, 6}});

System.out.println(Arrays.deepToString(def.getSquare()));
System.out.println(Arrays.deepToString(copy.getSquare()));
System.out.println(Arrays.deepToString(arr.getSquare()));

输出:

[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
[[1, 2, 3], [-1, -1, -1], [-10, -1, -1]]

【讨论】:

    猜你喜欢
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2019-01-18
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    相关资源
    最近更新 更多