如何让系统(BlueJ)按顺序分配唯一编号

发布于 2024-11-08 05:51:38 字数 1431 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Spring初心 2024-11-15 05:51:39

鉴于您正在使用 BlueJ,我会假设这是作业,所以我不会回答这样的问题,只是给出一些指示。

您需要在 File 对象中存储一个计数器,但是,该计数器需要在该对象的实例之间共享。该计数器可在构造时用于获取 File 的每个单独实例的编号,然后可以为相关实例存储该编号。


新更新中缺少的是序列号。您应该将其存储在静态变量中(以便在 File 的所有实例之间共享),然后将其当前值分配给构造函数中的成员变量,然后再为下一个实例化增加静态变量。

Seeing as you're using BlueJ, I'll assume it's homework, so I won't answer the question as such, just give some pointers.

You need to store a counter in the File object, but, that counter needs to be shared between instances of the object. This counter can be used at construction time to get the number for each individual instance of File, which can then be stored for the instance in question.


What you are missing in your new update is the sequence number. You should store this in a static variable (so that it is shared between all instances of File) and then assign its current value to a member variable in the constructor, before incrementing the static variable for the next instantiation.

枫以 2024-11-15 05:51:39

您将需要添加一个私有静态整数,您可以随意命名它。我将其称为numberOfFileInstances。在构造函数中,您需要将 numberOfFileInstances 加一。

这是我的示例:

public class File {

    private static int numberOfFileInstances = 0;

    public File() {
        File.numberOfFileInstances++;
    }

}

由于您使用 BlueJ,您将很容易看到每次创建新文件对象时,numberOfFileInstance 都会增加 1。在 BlueJ 中初始化 2 个(或任何大于 1 的数字)文件对象,然后双击该对象以调出检查器。单击“显示静态字段”按钮,您应该看到 private int numberOfFileInstance 以及您初始化的对象的计数。

You will need to add a private static integer, which you can call it whatever you want. I will call it numberOfFileInstances. In your constructor you will need to increment numberOfFileInstances by one.

Here is my example:

public class File {

    private static int numberOfFileInstances = 0;

    public File() {
        File.numberOfFileInstances++;
    }

}

Since your using BlueJ you'll be easily able to see that each time you create a new file object the numberOfFileInstance will be incremented by one. In BlueJ initialize 2 (or any number you would like grater than 1) File objects and double click on the object to bring up the inspector. Click on the "Show static fields" button and you should see private int numberOfFileInstance and the count of however many object you initialized.

つ可否回来 2024-11-15 05:51:39

如果您需要一个序列计数器,您可能需要考虑使用一个静态整数,为每个添加的文件递增该整数。

If you need a sequence counter, you might want to consider using a static integer which you increment for every file added.

故乡的云 2024-11-15 05:51:39

我不确定我明白你想要什么,但这听起来很简单:

public String toString()  { 
    return this.Name + " " + this.date + " " + this.type + " " +this.size;
} 

I'm not sure I understand what you want but it sounds simple:

public String toString()  { 
    return this.Name + " " + this.date + " " + this.type + " " +this.size;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文