无法使用父类中的变量?

时间:2023-01-23 22:33:43

So I need to have an example of inheritance in this code, and I am trying to get it so I instantiate private BufferedImage image in the parent class, and then have it take on different forms within the child classes such as Player, Enemy, and possibly Key. They will each have their specific image within them (also would this be a good example of polymorphism?). Here is the code:

所以我需要在这段代码中有一个继承的例子,我试图得到它,所以我在父类中实例化私有BufferedImage图像,然后让它在子类中使用不同的形式,如Player,Enemy和可能是关键。他们每个人都有自己的特定形象(这也是多态性的一个很好的例子吗?)。这是代码:

public class Entity { //hold things such as player, and values realted to that specific entity (nice especially for organization purposes)
Image i = new Image();
MazeModel model = new MazeModel();

private BufferedImage image; //they all have images
}

class Player extends Entity{
image = i.getPlayer();

public void setPlayerStart(){
    model.setPlayerX(50); //sets the starting x position of the player's image
    model.setPlayerY(50); //sets the starting y position of the player's image
}
}
//these next two I will possibly add later if I have the time
class Enemy extends Entity{
//nothing here for now
}

class Key extends Entity{
//nothing here for now
}

3 个解决方案

#1


private instances of class can't be inherited from parent to child class and you can't access in child class.

类的私有实例不能从父类继承到子类,也不能在子类中访问。

so private BufferedImage image; of Entity class can't be visible in Player class.

如此私密的BufferedImage图像;在Player类中无法看到Entity类。

Advice: Try making this private BufferedImage image; as protected BufferedImage image; so that you can access in your child(Player) class as well and your instance variable will also be secure.

建议:尝试制作这个私有的BufferedImage图像;作为受保护的BufferedImage图像;这样你就可以访问你的孩子(Player)课程,你的实例变量也是安全的。

#2


change private to public.

私人改变为公共。

public BufferedImage image;

public BufferedImage图像;

#3


I would use getter and setter to make it accessible.

我会使用getter和setter来使它可访问。

For your case, you can make every internal values private and access it via getter and setter:

对于您的情况,您可以将每个内部值设为私有,并通过getter和setter访问它:

public class Entity {
    private Image i;
    private MazeModel model;
    private BufferedImage image;

    // initialize these values when calling new
    public Entity() {
        this.i = new Image();
        this.model = new MazeModel();
    }

    // create getter and setter
    public BufferedImage getImage(){
        return this.image;
    }

    public void setImage(BufferedImage image){
        this.image = image;
    }

    public Image getI(){
        return this.i;
    }

    // other code...
}

class Player extends Entity {
    public Player(){
        this.setImage(getI().getPlayer());
    }

    //other code...
}

Other than getter and setter, you need to create Constructor (public Entity() and public Player()) to initialize values.

除了getter和setter之外,您还需要创建Constructor(public Entity()和public Player())来初始化值。

#1


private instances of class can't be inherited from parent to child class and you can't access in child class.

类的私有实例不能从父类继承到子类,也不能在子类中访问。

so private BufferedImage image; of Entity class can't be visible in Player class.

如此私密的BufferedImage图像;在Player类中无法看到Entity类。

Advice: Try making this private BufferedImage image; as protected BufferedImage image; so that you can access in your child(Player) class as well and your instance variable will also be secure.

建议:尝试制作这个私有的BufferedImage图像;作为受保护的BufferedImage图像;这样你就可以访问你的孩子(Player)课程,你的实例变量也是安全的。

#2


change private to public.

私人改变为公共。

public BufferedImage image;

public BufferedImage图像;

#3


I would use getter and setter to make it accessible.

我会使用getter和setter来使它可访问。

For your case, you can make every internal values private and access it via getter and setter:

对于您的情况,您可以将每个内部值设为私有,并通过getter和setter访问它:

public class Entity {
    private Image i;
    private MazeModel model;
    private BufferedImage image;

    // initialize these values when calling new
    public Entity() {
        this.i = new Image();
        this.model = new MazeModel();
    }

    // create getter and setter
    public BufferedImage getImage(){
        return this.image;
    }

    public void setImage(BufferedImage image){
        this.image = image;
    }

    public Image getI(){
        return this.i;
    }

    // other code...
}

class Player extends Entity {
    public Player(){
        this.setImage(getI().getPlayer());
    }

    //other code...
}

Other than getter and setter, you need to create Constructor (public Entity() and public Player()) to initialize values.

除了getter和setter之外,您还需要创建Constructor(public Entity()和public Player())来初始化值。