对象类和数组 - 为什么它返回“null” ? [java]
我编写了一个小类,它创建一个包含 3 个数组的报表对象。在创建对象时,这些数组用值进行初始化。然而,当我测试该类以查看例如部门数组中的内容时,它会打印出数组元素为空。为什么?
class Report
{
// declare instance variables (arrays)
public String[] departments = new String[4] ;
public double[] grossTotals = new double[4] ;
public double[] taxTotals = new double[4] ;
// constructor
public Report(){
// declare, create and initialise all in one statement
String[] departments = {"Accounting", "Sales", "HR", +
"Administration"} ;
double[] grossTotals = {0.0, 0.0, 0.0, 0.0} ;
double[] taxTotals = {0.0, 0.0, 0.0, 0.0} ;
} // END constructor
} // class Report
测试班
class TestReport
{
public static void main(String[] args) {
// create report object
Report r = new Report();
for (int i = 0; i <= 3 ; i++ )
{
System.out.println(r.departments[i]) ;
}
} //end main
} // end test class
谢谢巴巴
:
I wrote a small class that creates a report object containing 3 arrays. At creation of the object these arrays are initialised with values. However when i test the class to see for example what's in the departments array, it prints out that the array elements are null. why?
class Report
{
// declare instance variables (arrays)
public String[] departments = new String[4] ;
public double[] grossTotals = new double[4] ;
public double[] taxTotals = new double[4] ;
// constructor
public Report(){
// declare, create and initialise all in one statement
String[] departments = {"Accounting", "Sales", "HR", +
"Administration"} ;
double[] grossTotals = {0.0, 0.0, 0.0, 0.0} ;
double[] taxTotals = {0.0, 0.0, 0.0, 0.0} ;
} // END constructor
} // class Report
Test Class:
class TestReport
{
public static void main(String[] args) {
// create report object
Report r = new Report();
for (int i = 0; i <= 3 ; i++ )
{
System.out.println(r.departments[i]) ;
}
} //end main
} // end test class
thanks
Baba
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使它像这样
实际上,您正在创建构造函数本地的新数组对象,这些对象在构造函数中进行初始化。
您的类字段将使用上面的代码进行初始化。
如果您没有错误地完成此操作,请参阅此文档,它也会更好地阐明您的理解
**
更新
:**
上面的代码会给你非法的表达式开始
这是工作代码
Make it like this
Actually you are creating new arrays objects local to your constructor those are getting initialized in constructor.
your class fields will be initialized using the code above .
If you have not done it by mistake than please refer to this doc also it will better clear your understandings
**
Update
:**
Above code will give you illegal start of expression
Here is working code
正如其他答案所指出的,您的构造函数创建了新的局部变量“隐藏”实例变量,而不是用数据填充实例变量。
但是,如果将声明与填充分开,则填充代码会略有不同,并且他们没有完全正确地理解这一点。您还有一个不属于的“+”字符。
该代码可以编译并运行(经过测试),并且基本上可以满足您的要求。
您也可以在声明中创建数组,然后在构造函数或其他地方填充数组条目。
使用这种方法,代码可能类似于:
作为第三种选择,您可以在字段声明中执行所有初始化,如下所示:
在这种情况下,您根本不需要定义构造函数,因为它什么也不做。 Java 将提供一个空的。
As the other answers have noted, your constructor created new local variables "shadowing" the instance variables instead of populating the instance variables with data.
However, the population code is a little different if you separate the declaration from the populating, and they didn't get that quite right. You also had a '+' character that didn't belong.
This code compiles and works (tested), and does basically what your looking for.
You can alternatively create the arrays in the declaration and then populate the array entries in the constructor or elsewhere.
With that approach the code could be something like:
As a third alternative, you can do all the initialization in the field declaration as follows:
In this case, you don't need to define the constructor at all, as it does nothing. An empty one will be supplied by Java.