简体   繁体   English

如何创建两个相同但状态不同的对象

[英]How to create two of the same objects but in different states

I am trying to create at least two independent objects using the same class. 我试图使用同一类创建至少两个独立的对象。 Meaning that I want each of them to use the same class but use the variables in it independently. 这意味着我希望他们每个人都使用相同的类,但要独立使用其中的变量。 I am not sure if this is possible but that is why I am here. 我不确定这是否可能,但这就是为什么我在这里。

The two skeletons (skeleton1 and skeleton2) are the two objects that I want to be independent of each other. 两个骨架(skeleton1和skeleton2)是我要彼此独立的两个对象。

@Override
public void render(GameContainer gc, StateBasedGame gm, Graphics g)
        throws SlickException {

    background.draw(0, 0, Constants.WIN_WIDTH, Constants.WIN_HEIGHT);

    g.drawImage(ccb, mkX, mkY);

    Skeleton skeleton1 = new Skeleton(g, gm);
    Skeleton skeleton2 = new Skeleton(g, gm);

    score = new DeathScreenButton(scoreCounter, (Constants.WIN_WIDTH / 2) / 2, 0, g, Color.transparent);

}

More specifically, I want their x and y axis to be independent shown here (they are sX and sY): 更具体地说,我希望它们的x和y轴独立显示在此处(它们分别是sX和sY):

public class Skeleton {

private Graphics g;
private StateBasedGame gm;

private int mkX = InGame.getmkX();
private int mkY = InGame.getmkY();

// This makes them spawn on each other :(
// "static" prevents them from popping around the screen
private static int sX = (int)(Math.random() * Constants.WIN_WIDTH) + 1;
private static int sY = (int)(Math.random() * Constants.WIN_HEIGHT) + 1;

private Image skele;

public Skeleton (Graphics g, StateBasedGame gm) {

    this.g = g;
    this.gm = gm;

    try {
        init();
    } catch (SlickException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    render();

    try {
        update();
    } catch (SlickException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Your Skeletons are independent already. 您的骨骼已经是独立的。 One of Objected-Oriented programming purposes are class instances. 面向对象编程目的之一是类实例。 This way, two instances of the same class may have the same attributes, but they will never be the same . 这样,同一类的两个实例可能具有相同的属性,但是它们永远不会相同 It may be a little confusing in the beginning, but the objective is to develop reusable code, then you may instantiate 1..n Skeletons, and they will be independent. 一开始可能有些混乱,但是目标是开发可重用的代码,然后您可以实例化1..n Skeleton,它们将是独立的。

Skeleton skel1 = new Skeleton(Graphics g, StateBasedGame gm);

Hope it was helpful. 希望对您有所帮助。 Thank you. 谢谢。

Write/Overwrite the update method, if you haven't already. 编写/覆盖update方法(如果尚未安装)。 Update your positions in that method. 用这种方法更新您的职位。

Put the initialization of the variables sX and sY in the constructor. 把变量的初始化sXsY在构造函数中。

Remove static . 删除static By marking sX and sY static , all instances of your class will always share these values, so you can never have to Skeletons with different coordinates. 通过标记sXsY static ,你的类的所有实例将始终共享这些价值观,所以你可以永远不必骷髅使用不同的坐标。 If you change the value for one instance, you change it for all instances. 如果更改一个实例的值,则更改所有实例的值。

same class but use the variables in it independently 同一个类,但独立使用其中的变量

That basically means: I want to use instance variables. 这基本上意味着:我想使用实例变量。 Variables marked static are class variables. 标记为static的变量是变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 hashcode() 是根据 object 的地址创建 hashcode 的,那么两个内容相同的不同对象如何创建相同的 hashcode? - If the hashcode() creates hashcode based on the address of the object, how can two different objects with same contents create the same hashcode? 如何为同一个 Function 处理两个不同的对象 - How to Handle two different Objects for same Function 如何在同一JPanel中同时处理两个不同的对象? - How to handle two different objects in the same JPanel at the same time? 具有不同状态的相同SessionBean - Same SessionBeans with different states 如何在同一方法中执行两个不同的 ResultSet 对象 - How to execute two different ResultSet Objects in the same method 如何在Java中为两个不同的对象调用相同的方法 - How to call the same method for two different objects in Java 如何创建可用于两个不同对象的基类? - How to create a base class that works with two different objects? 通过合并集合中具有相同 Id 的两个对象来创建一个新对象,并将结果对象添加到不同的集合中 - Create a new object by merging two objects in a set having same Id and add the resulting object into a different set 如何使用不同的参数创建同一方法的两个实例 - How to create two instances of the same method with different parameters 如何从具有不同字段的同一类创建两个 json 文件 - How to create the two json file from same class with different fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM