Java:多对象变量(静态) [英] Java: Many-object variable ( static )

查看:85
本文介绍了Java:多对象变量(静态)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是面向对象编码的新手,我有以下问题。

I am new to object oriented coding and I have the following problem.

(请注意这个解决方案是我问题的一部分)

(note that this solution is part of my problem)

我需要一个许多对象可以引用的变量,但保留一些私有信息每个对象。更具体地说,我创建了一个名为 Worker 的类,我希望该类的每个对象都具有类型 int 。所以,第一个对象有 ID = 1 ,第二个 ID = 2 等...注意我不想要只是一个随机整数,而我需要从0开始计数并增加......

I need a variable that many objects can refer to, but keep some "private" information for each object. To be more specific, I created a class called Worker and I want every object of that class to have a unique ID of type int. So, first object has ID=1, second ID=2 etc... Note that I don't want just a random integer, whereas I need to start counting from 0 and increment...

在类中声明和初始化变量

Declaration and initialization of the variable in the class

static private int workId = 0;

我试图通过在构造函数体中添加这行代码来实现增量

and I tried to implement incremention by adding this line of code in the constructor body

workId++;

我实例化一些对象,将它们添加到 ArrayList 并使用for循环我打印每个对象的变量。

I instantiate some objects, add them to an ArrayList and using a for loop I print each object's variables.

System.out.println("Worker ID: "+workerList.get(i).getWorkerId());

getWorkerId()(类方法)包含

public int getWorkerId () { return this.workId; }

问题是我的程序不会打印每个唯一的 workID (因为没有),但是静态变量的最后一个值(恰好是对象的数量)。

Problem is my programm won't print every unique workID (because there isn't), but the last value of the static variable (which happens to be the number of objects).

你能描述一下吗?解决我的问题?

Can you describe a solution to my problem?

推荐答案

评论转为答案:

你走在正确的轨道上。

目前,您创建的每个工作人员都会增加您的静态成员。这意味着它不会保存工作者的ID(这不是这种情况,因为静态变量属于而不是单个实例),而是它拥有到目前为止您创建的工作人员总数

Currently, your static member will be increased with every worker you create. This means it will not hold a worker's ID (this can't be the case, as a static variable belongs to the class instead of individual instances), instead it holds the total number of workers you created so far.

修复


  • 将静态成员重命名为 numWorkers ,以便反映出它的作用

  • 介绍另一个(非静态)成员, id (或 workerId 如果您愿意)

  • workId ++ 更改为 id = ++ numWorkers

  • Rename your static member to something like numWorkers so it reflects what it does
  • Introduce another (non-static) member, id (or workerId if you prefer)
  • Change workId++ to id = ++numWorkers

为什么?

现在,每个工人都有自己的 id 。您可以通过查看当前创建的工作人员数( numWorkers )来分配它,将该值递增1,然后将其分配给 id 。这样,你的第一个工人的ID将是<$​​ c $ c> 1 ,第二个将是 2 ,依此类推。 numWorkers 始终保留创建的工作人员数量,这与您分发的最后一个ID同时发生。

Now, every worker has their own id. You assign it by looking at the current number of workers created (numWorkers), increment that value by one, then assign it to id. This way, your first worker's ID will be 1, the second will be 2 and so on. numWorkers always holds the number of workers created, which is at the same time the last ID you handed out.

汇总

由于这个问题得到了很多赞成(=兴趣),我会将上述内容概括为蓝图:

Since this question got quite some upvotes (= interest), I'll summarize the above into a blueprint:

public class Worker {
    private static int numWorkers; // Number of Worker objects created
    private int id;                // Individual ID of a worker

    public Worker() {
        id = ++numWorkers; // Will be 1 for the first, 2 for the second, ...
    }

    public int getID() {
        return id;
    }

    public static int getNumWorkers() {
        return numWorkers;
    }
}

请注意,在Java中,您不需要初始化原始类型 int 0 ,因为那是 int 的htmlrel =nofollow noreferrer>默认值。当然,无论如何都可以这样做 - 明确地说它永远不会伤害。

Note that in Java, you don't need to initialize the primitive type int to 0, as that's the default value for an int. Of course, it is fine to do it anyway - it never hurts to be explicit.

这篇关于Java:多对象变量(静态)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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