-1

/**文件类*/

public class File {
   // The File name.       
   private String name;
   // The date file created.     
   private String date ;
   // The type of file - audio, image, video, doc.     
   private String type;
   // The size of the file - 2MB, 2KB etc.     
   private String size; 


/**
 * Create a file 
 */

public File(String Filename)
{
name = Filename;
date = ("MM/dd/yyyy");
type = ();
size = ();
}


/**
 * Return the name of a File.
 */

public String getName()
{
return name;
}


/**
 * Update system number when called to the output terminal 
 */

public void Systemnumber ()
   name.increment();
   if(name.Filename() == 0) { // it jus rolled over !
      i.increment();
   }
   updateDisplay();
}


/**
 * Print File description to the output terminal 
 */

public void updateDisplay ()
public String s = String.format ("%02d", i); // gives you "001" 
for (int i = 001; i < 1000; i++) {String sequence = String.format("%02d", i); }
{      
return  (i + "", '$this.Name + " " + $this.date + " " + $this.type + " " +$this.size; }  


/**
 * This method is called everytime Increment the return value one, rolling over to zeor if the limit is reached 
 */

public void updateDisplay ()
{ displayString = name.
(
value = (value +1) % limit;
)
}
4

4 回答 4

1

看到你在使用 BlueJ,我会认为这是家庭作业,所以我不会这样回答这个问题,只是给出一些指示。

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


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

于 2011-05-18T15:49:57.033 回答
1

您将需要添加一个私有静态整数,您可以随意调用它。我会打电话numberOfFileInstances的。在您的构造函数中,您需要将 numberOfFileInstances 增加一。

这是我的例子:

public class File {

    private static int numberOfFileInstances = 0;

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

}

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

于 2011-11-10T06:33:21.560 回答
0

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

于 2011-05-19T21:16:25.620 回答
0

我不确定我是否理解您想要的,但听起来很简单:

public String toString()  { 
    return this.Name + " " + this.date + " " + this.type + " " +this.size;
} 
于 2011-05-18T14:52:55.310 回答