[英]assigning default constructor value from another constructor from different classes
I need to assign default values stored in my first class constructor to second class constructor following shows my second class from where i am calling Guest
class constructors default value. 我需要将存储在第一个类构造函数中的默认值分配给第二个类构造函数,以下显示了我从中调用
Guest
类构造函数的默认值的第二个类。
public class AuditoriumSeating {
Guest[][] seating;
public AuditoriumSeating(int rowNum, int columnNum) {
seating = new Guest[rowNum][columnNum];
Guest c;
for ( int i=0; i < rowNum; i++ ) {
for( int j=0; j < columnNum; j++ ) {
seating[i][j] = ;
}
}
}
}
But the problem is I can not figure out what to assign seating[i][j]
with, so I can assign this array seating[][]
with a default constructor value that I have assigned in my previous class constructor. 但是问题是我不知道该用什么来分配
seating[i][j]
,所以我可以用我在先前的类构造函数中分配的默认构造函数值将此数组seating[][]
分配。 Here is my guest class constructor: 这是我的来宾类构造函数:
public class Guest {
public String lastName;
public String firstName;
public Guest()
{
firstName="???";
lastName="???";
}
}
Guest
should assign whatever default you think best, perhaps null
: Guest
应分配您认为最佳的默认值,也许为null
:
public Guest() {
firstName = null;
lastName = null;
}
And your code creating it: 和您的代码创建它:
seating[i][j] = new Guest();
But : 但是 :
Guest
should probably provide a constructor that accepts the initial value to assign those as arguments Guest
可能应该提供一个接受初始值的构造函数,以将其分配为参数
public Guest(String _firstName, String _lastName) { firstName = _firstName; lastName = _lastName; }
Guest
's firstName
and lastName
probably shouldn't be public
; Guest
的firstName
和lastName
可能不应public
; instead, make them private
and use accessor functions. 而是将它们
private
并使用访问器函数。 Opinions can vary on this, but it's the overwhelming convention in Java, and don't worry, the JVM is very good at making those accessors efficient. 对此可能有不同的看法,但这是Java中压倒性的惯例,不用担心,JVM 非常擅长提高访问器的效率。
(Subjective) Opinions vary, but I find always explicitly using this
when referencing instance members helps avoid confusion between variables, arguments, and instance members: (主观的)观点各不相同,但是我发现在引用实例成员时始终明确使用
this
有助于避免变量,参数和实例成员之间的混淆:
public Guest(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }
Again, though, that's a matter of opinion and style. 同样,这是意见和风格的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.