gpt4 book ai didi

java - 其他对象内的对象的访问和声明

转载 作者:行者123 更新时间:2023-12-01 14:00:36 25 4
gpt4 key购买 nike

我是java新手,所以请原谅我的简单性。如何调用另一个对象中的对象(在本例中为 GCompound)。在下面的代码中,我想让“火焰”闪烁。但从run方法中似乎看不出来。

import java.awt.Color;
import acm.graphics.*;
import acm.program.*;

public class BuildTower extends GraphicsProgram {

GCompound Ekulobia;
GCompound Dike_na_fia;
GCompound Uli;
GCompound Aniche;
GCompound Achebe;
GCompound Akuwanta;
GCompound Ogbunike;
GCompound Obinohia;
GCompound Aguata;

private GCompound[] name = { Aguata, Ekulobia, Dike_na_fia, Uli, Aniche, Achebe, Akuwanta, Ogbunike, Obinohia } ;

public void run() {
setSize(800,300);
GLabel towerName;

for(int i = 0; i <= 8; i++ ) {
towerName = new GLabel (nameTower(i));
towerName.setFont("Times-12");
name[i] = new erectTower(towerName);
add(name[i],50 + i * 80,100);
}

for(int i = 0; i <= 8; i++ ) {
name[i].flame.setFilled(true); /*cannot access flame in erecttower method*/
pause (500);
name[i].flame.setFilled(false);
pause (700);
}
}

private String nameTower(int n) {
switch (n) {
case 0: return ("Aguata");
case 1: return ("Ekulobia");
case 2: return ("Dike na fia");
case 3: return ("Uli");
case 4: return ("Aniche");
case 5: return ("Achebe");
case 6: return ("Akuwanta");
case 7: return ("Ogbunike");
case 8: return ("Obinohia");
default: return null;
}
}
}

-

package myprojets;
import java.awt.Color;
import acm.graphics.*;

public class erectTower extends GCompound {

private static final Color RED = null;

private GPolygon base;
private GRect mast;
public GPolygon flame;

public erectTower(GLabel towerName) {
base = creatBase();
mast = new GRect(4.42,63.94 );
flame = createFlame();
add(base, 0,95);
add(mast,5.29,20.44);
add(flame,5.29,20.44);
add(towerName, (base.getWidth() - towerName.getWidth())/2 , 115 );
}

private GPolygon creatBase() {
GPolygon poly1 = new GPolygon();
poly1.addVertex(0.0, 0.0);
poly1.addEdge(5.29,-10.62);
poly1.addEdge(4.42,0.0);
poly1.addEdge(5.29,10.62);
poly1.addEdge(-15,0.0);
return poly1;
}

private GPolygon createFlame() {
GPolygon poly2 = new GPolygon();
poly2.addVertex(0.0,0.0);
poly2.addEdge(-5.29,-10.44);
poly2.addEdge(5.60,4.85);
poly2.addEdge(1.90,-4.85);
poly2.addEdge(1.9,4.85);
poly2.addEdge(5.6,-4.85);
poly2.addEdge(-5.29,10.44);
poly2.addEdge(-4.42,0.0);
poly2.setColor(Color.RED);
return poly2;
}
}

最佳答案

您有一个 GCompound 数组,而不是您创建的扩展 GCompound (erectTower) 的类。

更改:

private GCompound[] name = { Aguata, Ekulobia, Dike_na_fia, Uli, Aniche, Achebe, Akuwanta, Ogbunike, Obinohia } ;

private erectTower[] name = { Aguata, Ekulobia, Dike_na_fia, Uli, Aniche, Achebe, Akuwanta, Ogbunike, Obinohia } ;

以及对 GCompound 的所有其他引用,以便您使用自己的类。

关于java - 其他对象内的对象的访问和声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com