【问题标题】:Declare an Instance Variable to Reference an Object声明一个实例变量来引用一个对象
【发布时间】:2015-03-17 23:32:26
【问题描述】:

所以我们正在为游戏制作头像,并且我们已经将头像创建为Avatar 类文件。我们的问题是,在我们的类文件Game 中,我们很难将 Avatar 声明为实例变量。 代码如下:

public class Game extends JApplet {
    private static final long serialVersionUID = 1L;
    public static final long WIDTH = 800;
    public static final long HEIGHT = 600;

    // Declare an instance variable to reference your Avatar object

    public @Override void init() {
        // Store an instantiated Avatar into the instance variable
        setSize(new Dimension( WIDTH, HEIGHT));
        getContentPane().setBackground(Color.BLUE); 
    }

    public @Override void paint(Graphics g) { 
        super.paint(g); // Call the JAplet paint method (inheritance)
        // Call the draw method in your Avatar class
    }
}  

【问题讨论】:

  • 您是否按照 Oracle 网站上的教程路径进行操作?
  • 你的问题是什么?
  • 究竟是什么阻止了您为 Avatar 声明实例变量?抱歉,我真的看不到你在问什么。

标签: java object instance-variables japplet


【解决方案1】:

你可以试试:

private Avatar avatar;

avatar = new Avatar();

avatar.draw();

像这样:

public class Game extends JApplet {
private static final long serialVersionUID = 1L;
public static final long WIDTH = 800;
public static final long HEIGHT = 600;

// Declare an instance variable to reference your Avatar object
private Avatar avatar;


public @Override void init() {
    // Store an instantiated Avatar into the instance variable
    avatar = new Avatar();

    setSize(new Dimension( WIDTH, HEIGHT));
    getContentPane().setBackground(Color.BLUE); 
}

public @Override void paint(Graphics g) { 
    super.paint(g); // Call the JAplet paint method (inheritance)
    // Call the draw method in your Avatar class

    avatar.draw(); //replace "draw" with method you want to invoke :)
    }
}  

【讨论】:

    猜你喜欢
    • 2014-01-06
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2017-05-18
    • 2012-12-21
    • 2013-07-02
    相关资源
    最近更新 更多