在对象数组中=所有元素都相同...分配数据后 [英] In Object Array = all elements are same... After assign data

查看:82
本文介绍了在对象数组中=所有元素都相同...分配数据后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我创建对象数组 并关联数据...

For Example if I Create array of object And assing data...

问题的简短版本. array [0] .init("ce",2) array [1] .init("nh",2)

short version of problem. array[0].init("ce", 2) array[1].init("nh", 2)

输出...的数组[0] 将与array [1]相同 但为什么?怎么了?我需要..结果不一样

Output... Of array[0] Will be same as array[1] But why? what's wrong? I need.. not same results

这是代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.lang.Math;


public class Gra_ulamki {

    /**
     * @param args the command line arguments
     */

   public static ulamek[] tab_ulamkow;
   public static void main(String[] args) 
   {

         tab_ulamkow = new ulamek[30];


         tab_ulamkow[0] = new ulamek();
         tab_ulamkow[0].init("dd", 5);

         tab_ulamkow[1] = new ulamek();
         tab_ulamkow[1].init("dde", 8); 

         System.out.println("poz x --" + tab_ulamkow[0].x + "-- y poz " + tab_ulamkow[0].y);
          System.out.println("poz x --" + tab_ulamkow[1].x + "-- y poz " + tab_ulamkow[1].y);

         // TODO code application logic here
         //new GUI();
         //new GUI();
    }

}

class ulamek
{
public static String ch_v;
public static int x = 0, y = -5, y_max = 325;


public void init(String a, int number)
{
    this.ch_v = a;

   // przypisanie x
    this.x = number;    
}


public void move()
{


    // restart pozycji w osi y
    if(this.y < y_max)
    {
        this.y += +1;
    }
    else
    {
        this.y = -5;
    }

}

}

谢谢您的帮助

推荐答案

如果数据成员为static,则表示该类的所有实例都共享它:

If a data member is static, this means that it is shared by all instances of the class:

public static String ch_v;
public static int x = 0, y = -5, y_max = 325;

删除两个static修饰符.

这篇关于在对象数组中=所有元素都相同...分配数据后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆