Java 建造商';可以从其他方法访问的参数?
使构造函数的参数可供类中的不同方法访问的正确方法是什么 例如,在下面的代码片段中,我希望在名为aMethod的方法中使N可访问,而不更改aMethod的现有参数签名。myArray.length是最佳选择吗Java 建造商';可以从其他方法访问的参数?,java,class,Java,Class,使构造函数的参数可供类中的不同方法访问的正确方法是什么 例如,在下面的代码片段中,我希望在名为aMethod的方法中使N可访问,而不更改aMethod的现有参数签名。myArray.length是最佳选择吗 public class MyClass{ private int[][] myArray; public MyClass(int N){ if(N <= 0) throw new IndexOutOfBoundsException("Input E
public class MyClass{
private int[][] myArray;
public MyClass(int N){
if(N <= 0)
throw new IndexOutOfBoundsException("Input Error: N <= 0");
myArray = new int[N][N];
}
public void aMethod(int i, int j){
// N won't work here. Is myArray.length the best alternative?
if(i <= 1 || i > N)
throw new IndexOutOfBoundsException("Row index i out of bounds");
if(j <= 1 || j > N)
throw new IndexOutOfBoundsException("Column index j out of bounds");
}
}
公共类MyClass{
私有int[][]myArray;
公共MyClass(int N){
if(N只需为它创建一个字段,就像对数组所做的那样
public class MyClass{
private int[][] myArray;
private int myArraySize;
public MyClass(int N){
if(N <= 0)
throw new IndexOutOfBoundsException("Input Error: N <= 0");
myArray = new int[N][N];
myArraySize = N;
}
...
}
公共类MyClass{
私有int[][]myArray;
私有化;
公共MyClass(int N){
如果(N似乎“N”应该存储在类中的成员中。如果这样做,那么aMethod()方法也可以访问它
在任何情况下,您都应该在构造函数中调用需要构造函数参数的方法,或者将这些构造函数参数存储在成员变量中,并使它们可供其他方法使用。创建一个字段(看在上帝的份上,使用常用的命名约定命名它):
我认为IndexOutOfBoundsException
将在您不注意的情况下抛出,因为java在运行时检查数组边界。您是否需要此额外检查?为什么不使用数组myArray.length的length
?像我那样在类中添加一个新字段nSize
public class MyClass{
private int[][] myArray;
private int nSize;
public MyClass(int N){
if(N <= 0)
throw new IndexOutOfBoundsException("Input Error: N <= 0");
myArray = new int[N][N];
this.nSize= N;
}
公共类MyClass{
私有int[][]myArray;
私有化;
公共MyClass(int N){
如果(N您可以将其存储为另一个字段,但它已存储
public class MyClass{
private final int[][] myArray;
public MyClass(int n){
myArray = new int[n][n]; // will throw an exception if N < 0.
}
public void aMethod(int i, int j){
int n = myArray.length;
if(i < 0 || i >= n)
throw new IndexOutOfBoundsException("Index i out of bounds");
if(j < 0 || j >= n)
throw new IndexOutOfBoundsException("Column index j out of bounds");
}
}
公共类MyClass{
私有最终int[][]myArray;
公共MyClass(int n){
myArray=new int[n][n];//如果n<0,将引发异常。
}
公共空间法(int i,int j){
int n=myArray.length;
如果(i<0 | | i>=n)
抛出新的IndexOutOfBoundsException(“索引i超出范围”);
如果(j<0 | | j>=n)
抛出新的IndexOutOfBoundsException(“列索引j超出范围”);
}
}
当然,索引0和1对于数组是有效的。如果您不执行这些检查,您将得到一个IndexOutOfBoundException,但它会告诉您无效值是什么,这可能很有用。知道这两个索引中的哪一个是越界的总是很好。+1感谢您的两个选择。@Bohemian和PeterLawrey建议使用myArraySize+1的最后一个关键字是,你是对的。但有一点声音告诉我myArray.length更像是一个黑客,我想知道解决这个问题的最佳方法是什么。我是Java新手,但你的代码片段显示了波兰语和经验。再次感谢。@Anthony With 103k rep,你希望如此。。)我最喜欢的一句话是,完美的实现不是当没有什么可补充的时候,而是当没有什么可带走的时候。——法国作家安托万·德·圣埃克苏佩里(1900-1944)
public class MyClass{
private int[][] myArray;
private int nSize;
public MyClass(int N){
if(N <= 0)
throw new IndexOutOfBoundsException("Input Error: N <= 0");
myArray = new int[N][N];
this.nSize= N;
}
public class MyClass{
private final int[][] myArray;
public MyClass(int n){
myArray = new int[n][n]; // will throw an exception if N < 0.
}
public void aMethod(int i, int j){
int n = myArray.length;
if(i < 0 || i >= n)
throw new IndexOutOfBoundsException("Index i out of bounds");
if(j < 0 || j >= n)
throw new IndexOutOfBoundsException("Column index j out of bounds");
}
}