1

Java クラスでプログラムを利用する概念に問題があります。比較的単純なはずですが、難しいと思います。

「ライセンス プレートを生成するクラス LicensePlateFactory を記述します。一意のライセンス プレート番号 (int) を返すメソッド getNextPlate を提供します。LicencePlateFactory によって作成されるすべてのライセンス プレートには、100001 から始まる連番を使用します。

LicensePlateFactory を作成し、そこから取得して 12 個の連続するナンバー プレートを表示するプログラムを作成してください。」

私のコード(見栄えが悪く、かなり長い間問題を見つめた後、何をすべきかわかりませんでした):工場:

public class LicensePlateFactory 
{
    private int k = 100001;
    private int count;

    public LicensePlateFactory(int x)
    {
          this.count++;
    }

    public static void main(String[] args)
    {
    getnextPlate();
    }

    public int getnextPlate()
    {   
    return k + count;

    }
}

ファクトリを作成するプログラム:

public class LicensePlateFactoryRunner 
{

    private LicensePlateFactory fac;
    public LicensePlateFactoryRunner()
    {
        for (int x = 1; x < 13; x++)
        {
        LicensePlateFactory fac = new LicensePlateFactory();
                    System.out.println(LicensePlateFactory.getnextPlate());
        }
    }
}

おそらく、基本的なJavaの概念をいくつか忘れていますか?

4

2 に答える 2

0

Your code is going to display the same license plate 12 times. The reason is because you're creating a new LicensePlateFactory for every iteration of the loop. What you should do is move the creation of the factory to outside the loop.

Also, and this is a minor quibble, but usually loops start with 0 unless you have a really good reason to do otherwise.

于 2012-12-15T18:29:32.040 に答える
0

Here are some hints:

  1. Create the PlateFactory class with one field: the last used plate number and one method: getNextPlate() returning a Plate
  2. Introduce a Plate object with one field: plate number, as this object cannot be instantiated (assumed) without using the factory, it should have no public constructor. We can also assume the plate number cannot be changed (final field, no setter).
  3. Create an App class for your main() method and in this method create the PlateFactory instance, then use this instance 12 times to create 12 Plate (with number 100001, 100002, ... but the factory handles that).

Some reading:

于 2012-12-15T18:29:44.470 に答える