繁体   English   中英

访问对象数组项,java

[英]accessing an object array item, java

我已经使用数组语句创建了一个数字对象,并且可以在创建类时传递在类中传递的值,但是当我尝试从类(monogamegame类)外部检索元素值时,无法识别引用-如何正确引用?

public class monopolygame {

    public static void main(String[] args) {

        //set up array of 18 objects    
        property properties[] = new property[18];

        //create 18 property objects and populate array
        properties[0] = new property("a","available",400,500);//create property
        properties[1] = new property("b","available",400,500);//create property 
        properties[2] = new property("c","available",200,300);//create property 
        properties[3] = new property("d","available",100,180);//create property
        properties[4] = new property("e","available",400,700);//create property
    }
}

财产类别...

public class property
{
   public static void main(String[] args)
   {
   }

   //constructor
   public property(String propertyname, String owner, double price, double rent) 
   {
        System.out.println("Property info for " + propertyname 
                           + " - Rent : £" + rent 
                           + "Price : £" + price 
                           + "owned by :" + owner);
    }   
}

我在垄断类中使用这种参考来尝试访问数据

if (properties[2].propertyname == "available")
{
    System.out.println("avaialble");
}
else
{
    System.out.println("sold");
}

谢谢

您必须首先在“属性”类中声明这些属性:

class property  {
    String propertyname;
    String owner;
    int    price;
    int rent;
     public Property( String somename, String owner, int price, int rent ) {
         this.propertyname = somename;
         this.owner = owner;
         this.price = price;
         this.rent = rent;
         // and so on
      }
 }

您使用的数组是main方法的局部数组。

要在main方法范围之外访问它,您应该这样声明为class属性或instance属性:

public class monopolygame {
    public static property properties[]; 
    public static void main(String[] args) {
    //set up array of 18 objects

    properties = new property[18];
    .....

这样,您可以通过其他方法访问数组,如下所示:

    public void setUp() {
         for( property p : properties ) {
             System.out.println( p.propertyname ); // etc. 

然后你的代码:

if (properties[2].propertyname == "available")

将工作。

BTW在Java中,所有类名均按约定以大写字母开头,因此应为:

Property代替property ,用MonopolyGame代替monopolygame

给定您提供给我们的代码后,您似乎并没有真正存储传递给属性构造函数的值。 这有点接近您的属性类的外观:

public class property
{
    private String propertyname;
    private String owner;
    private double price;
    private double rent;

    public String getPropertyName()
    {
        return propertyname;
    }

    public void setPropertyName(string newName)
    {
        propertyname = newName;
    }

    // more getter/setter methods here

    public property(String propertyname, String owner, double price, double rent)//constructor
    {
        this.propertyname = propertyname;
        this.owner = owner;
        this.price = price;
        this.rent = rent;

        System.out.println("Property info for " + propertyname + " - Rent : £" + rent + "Price : £" + price + "owned by :" + owner);

    }
}

几点评论:

  • 在Java中,字符串比较需要使用equals()方法完成,而不是==。 有关为什么在某些情况下使用==可能会起作用的解释,请参见此链接 ,但这不是预期的。

  • 约定使用大写的类名->属性而不是属性。

  • 避免混合和匹配支架的位置。 在同一行的末尾或下一行的开始处使用,但不能同时使用。 最常使用的是同一行的结尾。

您需要添加一个公共方法来访问monopolygame类的内部。 那是您问题的主要方面。

但是总的来说,您的代码不是使用Java的正确方法。 例如,类名必须大写。 第二类中的空主干是毫无意义的。 您需要了解有关Java基本知识的更多信息,我回答了您的问题,因为我认为您可以在这里学到很多东西,但是我建议您检查一下有关Java Tutorial基础的内容。

显而易见,有两个问题:

  • 您没有将传递给property构造函数的参数存储在该类的字段中。
  • 完成此操作后,您将尝试按引用 (或按标识 ,通过== )而不是按值 (通过String#equals(String) )比较String#equals(String) 除非你已经通过实习字符串String#intern()两个不同的String具有相同的字符内容的情况下,不会通过比较平等== 该比较仅查看对象引用的内存地址,该地址很可能指向两个不同的 String实例,每个实例具有不同的地址。

由于该问题看起来像是一项家庭作业,因此请为其打上标签。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM