【问题标题】:objects classes and arrays - why is it returning 'null' ? [java]对象类和数组 - 为什么它返回 'null' ? [爪哇]
【发布时间】:2010-11-14 16:14:38
【问题描述】:

我写了一个小类来创建一个包含 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

谢谢

爸爸

【问题讨论】:

  • 顺便说一句,公开数组(如在 public String[] 中)是一个坏习惯。其他代码现在可以更改这些数组。即使public final 也不能解决这个问题,因为内容仍然可以更改。对于数组,最好尽可能将它们设为私有(也可能是 static final)。
  • 嗨,谢谢,这是正确的。但是,我需要一个 getter 方法来从我的示例没有的测试类中检索数据。

标签: java arrays class object


【解决方案1】:

变成这样

public Report(){
            // declare, create and initialise all in one statement
            this.departments = {"Accounting", "Sales", "HR", +
                                              "Administration"} ;
            this.grossTotals = {0.0, 0.0, 0.0, 0.0} ;
            this.taxTotals = {0.0, 0.0, 0.0, 0.0} ;

    } // END constru  

实际上,您正在创建构造函数本地的新数组对象,这些对象在构造函数中被初始化。

您的类字段将使用上面的代码进行初始化。

If you have not done it by mistake than please refer to this doc also it will better clear your understandings

**

更新

:** 上面的代码会给你非法的表达式开始

这是工作代码

 class  Report
 {
        // declare instance variables (arrays)
        public String[] departments = null;
        public double[] grossTotals = null;
        public double[] taxTotals = null;


        // constructor
        public Report(){
              this.departments = new String[]{"Accounting", "Sales", "HR", "Administration"} ;
         this.grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
         this.taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
    } // END constructor
 } 

【讨论】:

  • 也许很高兴补充一点,他遇到的问题被称为“场阴影”。也就是说,您创建与成员/实例变量同名的局部变量。
  • 非常感谢您的帮助!我进行了测试,似乎您的解决方案可以通过删除“=null”来进一步简化。似乎不需要。再次感谢!
  • @Baba thats true null 是可选的
【解决方案2】:

正如其他答案所指出的,您的构造函数创建了新的局部变量“隐藏”实例变量,而不是用数据填充实例变量。

但是,如果您将声明与填充分开,则填充代码会有所不同,并且它们并没有完全正确。您还有一个不属于您的“+”字符。

这段代码可以编译和工作(经过测试),基本上可以满足您的需求。

class  Report
{
       // declare instance variables (arrays)
       public String[] departments;
       public double[] grossTotals;
       public double[] taxTotals;


       // constructor
       public Report(){
           // populate instance variables
           departments = new String[]{"Accounting", "Sales", "HR",
                                             "Administration"} ;
           grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
           taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;

   } // END constructor
} // class  Report

您也可以在声明中创建数组,然后在构造函数或其他地方填充数组条目。

使用这种方法,代码可能类似于:

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(){
           // populate instance variable entries
           departments[0] = "Accounting";
           departments[1] = "Sales";
           departments[2] = "HR";
           departments[3] = "Administration";

           for (int i = 0; i < 4; i++) {
               grossTotals[i] = 0.0;
               taxTotals[i] = 0.0;

           }
   } // END constructor
} // class  Report

作为第三种选择,您可以在字段声明中进行所有初始化,如下所示:

class  Report
{
       // declare and populate instance variables (arrays)
       public String[] departments = new String[]{"Accounting", "Sales", "HR",
                                             "Administration"} ;
       public double[] grossTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;
       public double[] taxTotals = new double[]{0.0, 0.0, 0.0, 0.0} ;

} // class  Report

在这种情况下,您根本不需要定义构造函数,因为它什么都不做。 Java 将提供一个空的。

【讨论】:

  • 谢谢唐!我确实理解其他人指出的内容,但他们的解决方案不起作用。因此,解决方案是单独填充数组元素。令人惊讶的是,因为从句法上讲,我看不出填充数组的单行语句有什么问题。再次感谢! Baba(re +:这只是这篇文章的布局,但我知道我不需要它)
猜你喜欢
  • 2021-12-07
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
相关资源
最近更新 更多