我已創建使用陣列語句多個對象,我可以調用println,因爲它是創建的類內傳遞的值,但是當我嘗試和從類的外部檢索元素值( monopolygame類)它不承認refrence - 我怎麼才能正確引用?訪問對象數組項,JAVA
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");
}
感謝
你可以發佈什麼是**不**工作(我的意思是你怎麼嘗試使用它?) – OscarRyz 2009-11-26 00:54:58
究竟是什麼問題?如果它是你編譯的代碼,那麼請顯示該代碼。否則,目前還不清楚你實際上想要做什麼。 – 2009-11-26 00:55:07