如何在 super 关键字中使用单个 if 条件?

发布于 2025-01-12 06:43:10 字数 1306 浏览 0 评论 0原文

我正在尝试使用超级构造函数将下面的子类构造函数代码转换为一行代码。此构造函数只需要验证二维数组在两个维度上具有相同数量的元素,并将该信息传递给超类构造函数。我的超类构造函数具有三个参数,如下所示。我的子类构造函数只有一个数组参数。我也在这里附加了我的超类构造函数。 在方阵类中,我需要实现一个表示方阵的类。我需要检查矩阵中的行数必须与列数相同。我必须扩展 Matrix 类来实现我的 SquareMatrix 类。我不应该重写 Matrix 类的任何方法,但我确实需要创建新的构造函数: public SquareMatrix(double [][] array2D) 抛出 IllegalArgumentException 我的构造函数将一个新的方阵初始化为 rc by rc 矩阵,其中的值来自 array2D 副本。我的构造函数应该使用超类构造函数来完成所有工作。如果超类构造函数抛出异常,则 SquareMatrix 构造函数无需执行任何操作;异常将简单地传播给调用者。

public class Matrix {
int nRows;
int nCols;
double arr[][];
public Matrix(int numRows,int numCols,double [][] array2D)throws IllegalArgumentException{
    this.nRows=numRows;
    this.nCols=numCols;

if(nRows!=array2D.length || nCols!=array2D[0].length) throw new IllegalArgumentException("维度不匹配"); 别的 {

        this.arr = new double[nRows][nCols];
        for(int i=0;i<this.nRows;i++) {
            for(int j=0;j<this.nCols;j++) {
                this.arr[i][j]=array2D[i][j];
            }
            
            }
        }
    } 

} 公共类 SquareMatrix 扩展 Matrix {

public SquareMatrix(double[][] array2D) throws IllegalArgumentException{
 
  if(array2D.length==array2D[0].length) {
      super(array2D.length,array2D[0].length,array2D);
  }                    

I am trying to convert the below subclass constructor code to one line of code using the super constructor .This constructor only needs to verify that the 2D array has the same number of elements in both dimensions and pass that information to the super class constructor. My super class constructor has three parameters as given below. My subclass constructor has only one array parameter. I am attaching my super class constructor also here.
In the square matrix class, I need to implement a class that represents a square matrix. I need to check the number of rows in the matrix must be the same as the number of columns. I must extend the Matrix class to implement my SquareMatrix class. I should not override any of the methods of the Matrix class, but I do need to create new constructors:
public SquareMatrix(double [][] array2D) throws IllegalArgumentException
My constructor initializes a new square matrix as an rc by rc matrix with values copies from array2D. My constructor should use the super class constructor to do all the work. If the super class constructor throws an exception, the SquareMatrix constructor need do nothing; the exception will simply propagate to the caller.

public class Matrix {
int nRows;
int nCols;
double arr[][];
public Matrix(int numRows,int numCols,double [][] array2D)throws IllegalArgumentException{
    this.nRows=numRows;
    this.nCols=numCols;

if(nRows!=array2D.length || nCols!=array2D[0].length) throw new IllegalArgumentException("Dimensions are not matched");
else {

        this.arr = new double[nRows][nCols];
        for(int i=0;i<this.nRows;i++) {
            for(int j=0;j<this.nCols;j++) {
                this.arr[i][j]=array2D[i][j];
            }
            
            }
        }
    } 

}
public class SquareMatrix extends Matrix {

public SquareMatrix(double[][] array2D) throws IllegalArgumentException{
 
  if(array2D.length==array2D[0].length) {
      super(array2D.length,array2D[0].length,array2D);
  }                    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離人涙 2025-01-19 06:43:10

诀窍只是将 super 调用安排在子类构造函数的第一行。

public SquareMatrix(double[][] array2D){
  super(check(array2D), check(array2D), array2D);
}

private static int check(double[][] array2D) {
  if (array2D.length != array2D[0].length)
    throw new IllegalArgumentException("unsquare");
  return array2D.length;
} 

由于似乎没有其他方法可以正确构造该类,因此会引发异常。这是“方形或半身像”。

是的,我们做了两次相同的计算。我觉得不值得为了节省这么一点而费尽心思。

然而,对于这种情况似乎有一个更简单的解决方案:

public SquareMatrix(double[][] array2D){
  super(array2D.length, array2D[0].length, array2D);
  if (array2D.length != arrary2D[0].length)
    throw new IllegalArgumentException("unsquare");
}

没有理由必须首先检查合法性。

最后,检查有缺陷。它不会筛选出不规则的数组。没有什么可以阻止 array2D[0].length != array2D[1].length

The trick is simply to arrange that the super call can be in the first line of the subclass constructor.

public SquareMatrix(double[][] array2D){
  super(check(array2D), check(array2D), array2D);
}

private static int check(double[][] array2D) {
  if (array2D.length != array2D[0].length)
    throw new IllegalArgumentException("unsquare");
  return array2D.length;
} 

An exception is thrown since there seems to be no other way to construct the class correctly. It's "square or bust".

Yeah, we do the same computation twice. I didn't think it was worth working around, to save such a little.

However, there seems to be a simpler solution for this case:

public SquareMatrix(double[][] array2D){
  super(array2D.length, array2D[0].length, array2D);
  if (array2D.length != arrary2D[0].length)
    throw new IllegalArgumentException("unsquare");
}

There's no reason why the check for legitimacy has to be first.

Finally, the checking is flawed. It does not screen out ragged arrays. There's nothing stopping array2D[0].length != array2D[1].length.

胡大本事 2025-01-19 06:43:10

首先,简化。如果您只想从第三个参数中的实际数据获取 numRows 和 numCols 参数,则不需要它们。

然后在基类中创建一个名为 validate 或类似名称的抽象方法。

  private abstract void validate(double[][] data) throws MatrixValidationException

然后创建一个可以从您的验证方法抛出的新异常,足够有创意,MatrixValidationException。

public class MatrixValidationException extends Exception {
   public MatricValidationException(String reason) {
      super(reason);
   }
}

如果矩阵验证失败,请让您的子类 validate() 方法抛出异常,并提供验证失败原因的详细信息。

private validate(double[][] data) throws MatrixValidatioException {
   if (data.length == 0) {
      throw new MatrixValidationException("No data");
   }
   if (data.length != data[0].length) {
      throw new MatrixValidationException("Matrix is not square.");
   }
}

在构造函数中添加“抛出 MatrixValidationException”

public Matrix(double[][] data) throws MatrixValidationException {
   validate(data);
   arr = data;
}

public SquareMatrix(double[][]data) throws MatrixValidationException {
    super(data);
}

First, simplify. You don't need the numRows and numCols parameters if you're just going to get them from the actual data in the third parameter.

Then Create an abstract method in your base class called validate or something similar.

  private abstract void validate(double[][] data) throws MatrixValidationException

Then create a new Exception that can be thrown from your validate method, creatively enough, MatrixValidationException.

public class MatrixValidationException extends Exception {
   public MatricValidationException(String reason) {
      super(reason);
   }
}

If the matrix fails validation, have your subclass validate() method throw an exception with the details of why the validation failed.

private validate(double[][] data) throws MatrixValidatioException {
   if (data.length == 0) {
      throw new MatrixValidationException("No data");
   }
   if (data.length != data[0].length) {
      throw new MatrixValidationException("Matrix is not square.");
   }
}

Add a "throws MatrixValidationException" to your constructors

public Matrix(double[][] data) throws MatrixValidationException {
   validate(data);
   arr = data;
}

public SquareMatrix(double[][]data) throws MatrixValidationException {
    super(data);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文