简体   繁体   English

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

[英]How to use the single if condition inside the super keyword?

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.我正在尝试使用超级构造函数将下面的子类构造函数代码转换为一行代码。此构造函数只需要验证二维数组在两个维度上具有相同数量的元素并将该信息传递给超级构造函数 class。 My super class constructor has three parameters as given below.我的超级 class 构造函数具有三个参数,如下所示。 My subclass constructor has only one array parameter.我的子类构造函数只有一个数组参数。 I am attaching my super class constructor also here.我也在这里附上我的超级 class 构造函数。 In the square matrix class, I need to implement a class that represents a square matrix.在方阵 class 中,我需要实现一个表示方阵的 class。 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.我必须扩展矩阵 class 来实现我的 SquareMatrix class。我不应该覆盖矩阵 class 的任何方法,但我确实需要创建新的构造函数: public SquareMatrix(double [][] array2D) throws IllegalArgumentException 我的构造函数初始化一个新方矩阵作为 rc by rc 矩阵,其值从 array2D 复制。 My constructor should use the super class constructor to do all the work.我的构造函数应该使用超级 class 构造函数来完成所有工作。 If the super class constructor throws an exception, the SquareMatrix constructor need do nothing;如果超级 class 构造函数抛出异常,则 SquareMatrix 构造函数不需要做任何事情; 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"); if(nRows.=array2D.length || nCols;=array2D[0].length) throw new IllegalArgumentException("维度不匹配"); 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 { } 公共 class SquareMatrix 扩展矩阵 {

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

The trick is simply to arrange that the super call can be in the first line of the subclass constructor.诀窍是简单地安排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;
} 

An exception is thrown since there seems to be no other way to construct the class correctly.抛出异常,因为似乎没有其他方法可以正确构造 class。 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 .它不会筛选出参差不齐的 arrays。没有什么可以阻止array2D[0].length.= array2D[1].length

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.如果您只是要从第三个参数中的实际数据中获取它们,则不需要 numRows 和 numCols 参数。

Then Create an abstract method in your base class called validate or something similar.然后在您的基础 class 中创建一个名为 validate 或类似方法的抽象方法。

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

Then create a new Exception that can be thrown from your validate method, creatively enough, MatrixValidationException.然后创建一个可以从您的验证方法中抛出的新异常,足够有创意,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.如果矩阵验证失败,请让您的子类 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.");
   }
}

Add a "throws MatrixValidationException" to your constructors向构造函数添加“throws MatrixValidationException”

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM