Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 声明引用对象的实例变量_Java_Object_Instance Variables_Japplet - Fatal编程技术网

Java 声明引用对象的实例变量

Java 声明引用对象的实例变量,java,object,instance-variables,japplet,Java,Object,Instance Variables,Japplet,因此,我们正在为一个游戏制作一个化身,并且我们已经将该化身创建为一个化身类文件。我们的问题是,在类文件游戏中,我们很难将Avatar声明为实例变量。 代码如下: public class Game extends JApplet { private static final long serialVersionUID = 1L; public static final long WIDTH = 800; public static final long HEIGHT =

因此,我们正在为一个游戏制作一个化身,并且我们已经将该化身创建为一个化身类文件。我们的问题是,在类文件游戏中,我们很难将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
    }
}  
你可以试试:

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 :)
    }
}  

您是否遵循了Oracle网站上的教程路径?您的问题是什么?到底是什么阻止您为Avatar声明实例变量?对不起,我真的看不出你在问什么。