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.
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.
发布评论
评论(4)
鉴于您正在使用 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.
您将需要添加一个私有静态整数,您可以随意命名它。我将其称为
numberOfFileInstances
。在构造函数中,您需要将 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:
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.
如果您需要一个序列计数器,您可能需要考虑使用一个静态整数,为每个添加的文件递增该整数。
If you need a sequence counter, you might want to consider using a static integer which you increment for every file added.
我不确定我明白你想要什么,但这听起来很简单:
I'm not sure I understand what you want but it sounds simple: