2013-10-15 97 views
0

我是新来的java所以请原谅我的简单。 在这种情况下,如何调用另一个对象中的对象GCompound。在下面的代码中,我想让“火焰”闪烁。但它似乎从运行方法中看不到。访问和声明其他对象内的对象

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; 
    } 
} 
+0

“眨眼”是邪恶的。 – 2013-10-15 08:22:56

+0

Appart从这个不好的代码风格,它应该工作。什么是错误?我猜'NullPointerException'? –

+0

忘记'NullPointerException'的想法。刚刚发现你的数组初始化。但仍然:确切的错误是什么? –

回答

1

你的GCompound秒的数组,而不是创建一个扩展GCompounderectTower)类。

变化:

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让你使用你自己的类的所有其他引用。

+0

完全错过了那个,谢谢! –

+0

如果您发现我的答案足够,请将其标记为已解决! – Michael

+0

我不是OP ;-)只是想知道这个自己。 –

0

虽然@迈克尔是正确的,我试图解释这一点。您的name阵列属于GCompound。这意味着您通过name[i]访问的任何内容都具有静态类型GCompund。这意味着即使运行时的动态类型为erectTower,您也只能访问GCompund的成员。让我们使这用一个局部变量更加清晰:

GCompound foo = name[i]; // works 
foo.flame.setFilled(true); // won't work because foo is GCompound 

erectTower bar = name[i]; // won't work without cast because array is of type GCompound 
bar.flame.setFilled(true); // would work if bar was correctly assigned before 

你可以改变你的阵列的类型为@迈克尔建议,或者你可以投你的变数。

erectTower bar = (erectTower)name[i]; // works as long as all objects in your array are of type erectTower. Else it would throw a ClassCastException 
bar.flame.setFilled(true) // works 

您可能要阅读polymorphism and static/dynamic type