扩展抽象类

吉赫

我目前在抽象类方面遇到麻烦。

有人告诉我,我需要:“创建一个名为” Sprite“的抽象类,该抽象类存储一个(x,y)位置以及一个对Image的引用。您的Sprite类的构造函数应采用2个int参数(以指定初始x和y位置),以及一个字符串(该字符串是要从中加载图像的文件的名称),它应该加载图像(而不是Turtle类加载图像)。 Sprite类中的一个抽象方法,名为“ public void update(Graphics g)”。此函数应该没有主体。其目的是确保从该类继承的任何类都实现一个update函数。

我想我已经了解了如何制作抽象类。我也认为我对Sprite类的构造函数中的参数没问题。但是,我从来没有在Java中加载图像,也不确定如何扩展/实现抽象类函数。我做的正确吗?以及如何从Turtle构造函数中正确调用该抽象类,以便正确设置变量?修改后,仍然有麻烦:

    class Turtle extends Sprite
{
    private int x;
    private int y;
    private static int dest_x;
    private static int dest_y;
    private String image;

   // private Sprite sprite;


 Turtle() {
           // super();
            super(x, y, image);
            image = "turtle.png";

          }

更新的精灵类

import java.awt.Graphics;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

public abstract class Sprite
{

int x;
int y;
String imageName;

Image image;

Sprite(){}

public Sprite(int x1, int y1, String im)
{

//Store variables


    imageName = im;
    x1 = x;
    y1 = y;

    try {
        image = ImageIO.read(new File(imageName));

    } catch (IOException ioe) {
        System.out.println("Unable to load image file.");

    }
}


public abstract void update(Graphics g);

public Image getImage()
{

    return image;

}



}

以下是一些代码示例:

import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

class Turtle extends Sprite
{
    private int x;
    private int y;
    private static int dest_x;
    private static int dest_y;
    private String image;

   // private Sprite sprite;


 Turtle() {

            image = "turtle.png";

          }

    public int getX() { return x; }
    public int getY() { return y; }

    public void setX(int xIn) { x = xIn; }
    public void setY(int yIn) { y = yIn; }

    public void update(Graphics g) {
        // Move the turtle
        if (x < dest_x) {
            x += 1;
        } else if (x > dest_x) {
            x -= 1;
        }

        if (y < dest_y) {
            y += 1;
        } else if (y > dest_y) {
            y -= 1;
        }

        // Draw the turtle


        g.drawImage(image, x, y, 100, 100, null);



    }

import java.awt.Graphics;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

public abstract class Sprite
{

int X;
int Y;
String image;

Image Image;

Sprite(){}

public Sprite(int x, int y, String im)
{

//Store variables


    image = im;
    X = x;
    Y = y;

    try {
        Image = ImageIO.read(new File(image));

    } catch (IOException ioe) {
        System.out.println("Unable to load image file.");

    }
}
巴伦德

您进展顺利。Sprite的构造函数看起来正确。回顾您的问题:

但是,我从来没有在Java中加载图像,也不确定如何扩展/实现抽象类函数。

ImageIO.read呼叫将为您加载图像。Sprite构造函数很好。

以及如何从Turtle构造函数中正确调用该抽象类,以便正确设置变量?

在子类的构造函数中,可以调用super( ... )从父类(超类)调用构造函数。

例子:

public abstract class Fruit {
    public Fruit(String name) {
       //stuff...
    }

    public void someMethod1() { }
}

public class Lemon extends Fruit {
    public Lemon() {
        // child class constructor calling superclass constructor
        super("John");

        // child class constructor calling own method:
        someMethod2();

        // child class constructor calling superclass method:
        someMethod1();
    }

    public void someMethod2() { }
}

要调用超类的任何其他方法,您可以像平常一样键入其名称。可以这么说,超类和子类方法列表被“合并”。可以将其想象成将两个蓝图彼此叠加,然后将它们放在灯前。在我的示例中,“柠檬”类具有两个方法:someMethod1()someMethod2()如果我创建了一个“ Apple”类而没有添加任何自己的方法,那么它只会具有someMethod1()

后续问题更新:

您遇到了Cannot reference Sprite.x before supertype constructor has been called在调用超级构造方法时,这是一个特殊的限制。它与构造函数运行时的步骤顺序有关。

您会记住,对象是类的实例。类就像对象包含什么内容的蓝图。类具有一个父类,并且在创建对象时,它包含其类的内容以及其所有父类彼此层叠。

在对象构建期间,此分层很重要,因为构造函数首先在这些层(父级)的所有位置进行钻取,然后再向上钻取。它更容易用一个例子来说明这一点,所以这里会发生什么*当柠檬的一个对象,从我刚才的回答是构造:

1. Lemon constructor call
   2. Fruit constructor call (super-constructor of Lemon)
      3. java.lang.Object constructor call (super-constructor of Fruit)
      4. java.lang.Object constructor body
      5. Everything at the java.lang.Object inheritance level now ready to use.
   6. Fruit constructor body
   7. Everything at the Fruit inheritance level now ready to use.
8. Lemon constructor body
9. Everything at the Lemon inheritance level now ready to use.

*):此列表省略了对象创建过程中发生的其他一些事情,但涵盖了我需要答案的部分。

当您执行此序列的步骤2时,会发生错误。您的代码正在尝试访问this.x,直到第8步才可以访问。

这意味着您需要在其他地方获取值。您有几种选择:

  1. 只需在此处输入值作为文字即可。
  2. 使用您自己的参数列表中的值。
  3. 使用定义为static变量/常量或static方法返回的值

对于您的问题代码,您不能使用2.,因为您的Turtle构造函数没有参数。最简单的选项将是选项1:只需将所需的值放在super(之间)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章