简体   繁体   中英

Java Array of objects' values

import java.awt.*;
import hsa.Console;
public class Game{
    static Console c;
    public static void Wait (int time){
        try{
            Thread.sleep (time);
        }
        catch (InterruptedException e){
        }
    }
    public static class Tile{
        public int x,y,stack;
        public Tile(){
            x = 0;
            y = 0;
            stack = 0;
        }
        public Tile(int xco, int yco, int stacknum){
            x = xco;
            y = yco;
            stack = stacknum;
        }
        public void draw(Tile tile){ //To draw the tile
            if(stack>0){
                c.setColor(Color.red);
                c.fillRect(x*78+1+x,y*78+1+y,78,78); //Calculate coordinates
            }
            else{
                c.setColor(Color.blue);
                c.fillRect(x*78+1+x,y*78+1+y,78,78);
            }
        }
    }
    public static void main (String[] args){
        c = new Console ();

        for(int i=0;i<640;i+=79) c.drawLine(i,0,i,474);
        for(int i=0;i<500;i+=79) c.drawLine(0,i,632,i);
        //8x6 tiling
        Tile[][] tile = new Tile[8][6];
        for(int i=0;i<8;i++){
            for(int j=0;j<6;j++){
                tile[i][j] = new Tile();
                tile[i][j].x = i;
                tile[i][j].y = j; //Set x and y coordinates
                tile[i][j].stack = 5;
            }
        }
        Tile.draw(tile[0][0]);
    }
}

Here I have a tile of 8x6 squares using a multidimensional array. I would think that the coordinates would correspond to the correct numbers, but for some reason the coordinates seem to copy the ones created before it. Could someone tell me why this is happening and how the code should be corrected? Btw I started java so I'm not completely used to object oriented programming :P

Your co-ordinates are declared as static:

public static int x,y,stack;

Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class , rather than with any object. Every instance of the class shares a static class variable, which is in one fixed location in memory.

You should however remove the static modifier to have specific value for each object of Tile

Edit: From your below comment and for your better understanding, to work with draw() function:

Approach 1 : If we wish to have draw function to be static :

public static void draw(Tile tile){ //To draw the tile
            if(stack>0){
                c.setColor(Color.red);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
                   //Calculate coordinates
            }
            else{
                c.setColor(Color.blue);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
            }
        }

You can call this function by Tile.draw(tile) ; where tile is an instance of Tile

Approach 2: If draw(Title tile) function is non static: you don't need to pass the tile instance at all:

public  void draw(){ //To draw the tile
            if(stack>0){
                c.setColor(Color.red);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
                   //Calculate coordinates
            }
            else{
                c.setColor(Color.blue);
                c.fillRect(tile.x*78+1 + tile.x, tile.y*78+1+tile.y, 78, 78); 
            }
        }

Then create an instance Tile title = new Title() and call title.draw()

您已将x,y,z标记为静态,因此您的类的所有实例都使用相同的实例,只需删除static关键字

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM