我正在使用两个单独的类document.obj_data = {
"earnings_announcements_earnings_table" :
[ [ "10/26/2017", "9/2017", "$0.06", "--", "--", "--", "--" ] , [ "7/27/2017", "6/2017", "$1.40", "$0.40", "<div class=\"right neg negative neg_icon showinline down\">-1.00</div>", "<div class=\"right neg negative neg_icon showinline down\">-71.43%</div>", "After Close" ] , [ "4/27/2017", "3/2017", "$1.03", "$1.48", "<div class=\"right pos positive pos_icon showinline up\">+0.45</div>", "<div class=\"right pos positive pos_icon showinline up\">+43.69%</div>", "After Close" ] , [ "2/2/2017", "12/2016", "$1.40", "$1.54", "<div class=\"right pos positive pos_icon showinline up\">+0.14</div>", "<div class=\"right pos positive pos_icon showinline up\">+10.00%</div>", "After Close" ] , [ "10/27/2016", "9/2016", "$0.85", "$0.52", "<div class=\"right neg negative neg_icon showinline down\">-0.33</div>", "<div class=\"right neg negative neg_icon showinline down\">-38.82%</div>", "After Close" ] , [ "7/28/2016", "6/2016", "$1.14", "$1.78", "<div class=\"right pos positive pos_icon showinline up\">+0.64</div>", "<div class=\"right pos positive pos_icon showinline up\">+56.14%</div>", "After Close" ] , [ "4/28/2016", "3/2016", "$0.61", "$1.07", "<div class=\"right pos positive pos_icon showinline up\">+0.46</div>", "<div class=\"right pos positive pos_icon showinline up\">+75.41%</div>", "After Close" ] , [ "1/28/2016", "12/2015", "$1.61", "$1.00", "<div class=\"right neg negative neg_icon showinline down\">-0.61</div>", "<div class=\"right neg negative neg_icon showinline down\">-37.89%</div>", "After Close" ] , [ "10/22/2015", "9/2015", "-$0.1", "$0.17", "<div class=\"right pos positive pos_icon showinline up\">+0.27</div>", "<div class=\"right pos positive pos_icon showinline up\">+270.00%</div>", "After Close" ] , [ "7/23/2015", "6/2015", "-$0.15", "$0.19", "<div class=\"right pos positive pos_icon showinline up\">+0.34</div>", "<div class=\"right pos positive pos_icon showinline up\">+226.67%</div>", "After Close" ] , [ "4/23/2015", "3/2015", "-$0.13", "-$0.12", "<div class=\"right pos positive pos_icon showinline up\">+0.01</div>", "<div class=\"right pos positive pos_icon showinline up\">+7.69%</div>", "After Close" ] , [ "1/29/2015", "12/2014", "$0.24", "$0.45", "<div class=\"right pos positive pos_icon showinline up\">+0.21</div>", "<div class=\"right pos positive pos_icon showinline up\">+87.50%</div>", "After Close" ] , [ "10/23/2014", "9/2014", "-$0.73", "-$0.95", "<div class=\"right neg negative neg_icon showinline down\">-0.22</div>", "<div class=\"right neg negative neg_icon showinline down\">-30.14%</div>", "After Close" ] , [ "7/24/2014", "6/2014", "-$0.13", "-$0.27", "<div class=\"right neg negative neg_icon showinline down\">-0.14</div>", "<div class=\"right neg negative neg_icon showinline down\">-107.69%</div>", "After Close" ] , [ "4/24/2014", "3/2014", "$0.22", "$0.23", "<div class=\"right pos positive pos_icon showinline up\">+0.01</div>", "<div class=\"right pos positive pos_icon showinline up\">+4.55%</div>", "After Close" ] , [ "1/30/2014", "12/2013", "$0.68", "$0.51", "<div class=\"right neg negative neg_icon showinline down\">-0.17</div>", "<div class=\"right neg negative neg_icon showinline down\">-25.00%</div>", "After Close" ] , [ "10/24/2013", "9/2013", "-$0.09", "-$0.09", "<div class=\"right pos_na showinline\">0.00</div>", "<div class=\"right pos_na showinline\">0.00%</div>", "After Close" ] , [ "7/25/2013", "6/2013", "$0.04", "-$0.02", "<div class=\"right neg negative neg_icon showinline down\">-0.06</div>", "<div class=\"right neg negative neg_icon showinline down\">-150.00%</div>", "After Close" ] , [ "4/25/2013", "3/2013", "$0.10", "$0.18", "<div class=\"right pos positive pos_icon showinline up\">+0.08</div>", "<div class=\"right pos positive pos_icon showinline up\">+80.00%</div>", "After Close" ] , [ "1/29/2013", "12/2012", "$0.28", "$0.21", "<div class=\"right neg negative neg_icon showinline down\">-0.07</div>", "<div class=\"right neg negative neg_icon showinline down\">-25.00%</div>", "After Close" ] , [ "10/25/2012", "9/2012", "-$0.08", "-$0.23", "<div class=\"right neg negative neg_icon showinline down\">-0.15</div>", "<div class=\"right neg negative neg_icon showinline down\">-187.50%</div>", "After Close" ] , [ "7/26/2012", "6/2012", "--", "--", "--", "--", "After Close" ] , [ "4/26/2012", "3/2012", "--", "--", "--", "--", "After Close" ] , [ "1/31/2012", "12/2011", "--", "--", "--", "--", "After Close" ] , [ "10/25/2011", "9/2011", "--", "--", "--", "--", "After Close" ] , [ "7/26/2011", "6/2011", "--", "--", "--", "--", "After Close" ] , [ "4/26/2011", "3/2011", "--", "--", "--", "--", "--" ] , [ "1/27/2011", "12/2010", "--", "--", "--", "--", "After Close" ] , [ "10/21/2010", "9/2010", "--", "--", "--", "--", "After Close" ] , [ "7/22/2010", "6/2010", "--", "--", "--", "--", "After Close" ] , [ "4/22/2010", "3/2010", "--", "--", "--", "--", "After Close" ] , [ "1/28/2010", "12/2009", "--", "--", "--", "--", "After Close" ] , [ "10/22/2009", "9/2009", "--", "--", "--", "--", "After Close" ] , [ "7/23/2009", "6/2009", "--", "--", "--", "--", "After Close" ] ]
和Animal
。 Room
实例化,其中包含数组。然后将Room
个对象实例化并放置在Animal
内的数组中。如何为Room
个对象提供对Animal
所放置的内容的引用,以便当我对它们调用Room
方法时,它们可以返回它们所在房间的名称?
look()
-
public static void main(String[] args) {
Room mainRoom = new Room("The Lobby");
Animal gizmo = new Animal("Gizmo");
mainRoom.addAnimal(gizmo);
System.out.println(mainRoom.toString());
gizmo.look();
}
-
public class Room {
private String name;
private Animal[] animals;
private Room currentRoom;
int i = 0;
public Room(String name) {
setName(name);
animals = new Animal[10];
}
public String toString() {
String temp = new String();
temp += "\nThis room is " + name + ".\n\n";
temp += "In the lobby are the following animals: \n";
for (Animal s : animals) {
temp += s.toString() + "\n";
}
return temp;
}
public void setName(String name) {
this.name = name;
}
public void addAnimal(Animal a) {
if (i < 10) {
if (animals[i] != null) {
i++;
addAnimal(a);
} else {
animals[i] = a;
}
} else {
System.out.println("Room full");
}
}
}
答案 0 :(得分:2)
如何为Animal对象提供他们所在房间的参考 当我对它们调用look()方法时,它们可以返回 他们所在房间的名称?
您应该在Animal中设置房间:
Animal gizmo = new Animal("Gizmo");
gizmo.setCurrentRoom(mainRoom); // this would set mainRoom as the attribute of gizmo
此处您的代码:
public void look() {
System.out.println(name + " is currently in " + getCurrentRoom());
//getCurrentRoom().toString();
}
会获取上面代码设置的currentRoom
。
答案 1 :(得分:1)
我认为最优雅的解决方案将在Room的addAnimal函数中,如此
public void addAnimal(Animal a) {
if (i < 10) {
if (animals[i] != null) {
i++;
addAnimal(a);
} else {
animals[i] = a;
a.setCurrentRoom(this); // Setting room of animal
}
} else {
System.out.println("Room full");
}
}
答案 2 :(得分:1)
gizmo.setCurrentRoom(mainRoom);
(如果在Animal
中构造Room
对象,请使用:gizmo.setCurrentRoom(this)
)
您也可以将String getRoomName() {return currentRoom.getName();}
添加到Animal
课程。