如何在 super 关键字中使用单个 if 条件?
我正在尝试使用超级构造函数将下面的子类构造函数代码转换为一行代码。此构造函数只需要验证二维数组在两个维度上具有相同数量的元素,并将该信息传递给超类构造函数。我的超类构造函数具有三个参数,如下所示。我的子类构造函数只有一个数组参数。我也在这里附加了我的超类构造函数。 在方阵类中,我需要实现一个表示方阵的类。我需要检查矩阵中的行数必须与列数相同。我必须扩展 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
诀窍只是将 super 调用安排在子类构造函数的第一行。
由于似乎没有其他方法可以正确构造该类,因此会引发异常。这是“方形或半身像”。
是的,我们做了两次相同的计算。我觉得不值得为了节省这么一点而费尽心思。
然而,对于这种情况似乎有一个更简单的解决方案:
没有理由必须首先检查合法性。
最后,检查有缺陷。它不会筛选出不规则的数组。没有什么可以阻止
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.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:
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
.首先,简化。如果您只想从第三个参数中的实际数据获取 numRows 和 numCols 参数,则不需要它们。
然后在基类中创建一个名为 validate 或类似名称的抽象方法。
然后创建一个可以从您的验证方法抛出的新异常,足够有创意,MatrixValidationException。
如果矩阵验证失败,请让您的子类 validate() 方法抛出异常,并提供验证失败原因的详细信息。
在构造函数中添加“抛出 MatrixValidationException”
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.
Then create a new Exception that can be thrown from your validate method, creatively enough, MatrixValidationException.
If the matrix fails validation, have your subclass validate() method throw an exception with the details of why the validation failed.
Add a "throws MatrixValidationException" to your constructors