-1

だから私はこのプロジェクトに取り組んでおり、あるクラスから別のクラスに変数を移動するのに問題があります。この小さなソフトウェアの目標は、ユーザーに 2 つの情報を入力させることです。1 つは文字列で、もう 1 つは int です。また、情報を取得するクラスと計算するクラスの 2 つのクラスがあります。これは Software と呼ばれる最初のクラスです:

import java.util.Scanner;

public class Software
{
public Scanner softwareName;
public Scanner devicesAmount;


public Software()
{
    devicesAmount = new Scanner(System.in);
    softwareName = new Scanner(System.in);
}

/**
*Gets the information from the user to later on process
*/
public void InfoGet()
{
    Devices findDevices= new Devices();
    Devices findNumber= new Devices();

    String softwareName;
    int devicesAmount;
    Scanner sc= new Scanner(System.in);
    System.out.println("Welcome to the Software License Calculator");
    System.out.println("Please type in the name of the Software:");

    softwareName = sc.nextLine();

    System.out.println("");

    System.out.println("Please type in the number of devices that have the software:");
    devicesAmount=sc.nextInt();

    findDevices.Calculations(devicesAmount);
    findNumber.getNewDevices();

    sc.close();
    System.out.println(softwareName+ " & "+ findNumber);
   }
}

次に、Devices という 2 番目のクラスを示します。

public class Devices
{
public int NewDevices;
public String softwareName;

   /**
    * Gets number of Devices to preform the calculations of removing 1000 users
    */
    public static void Devices()
    {
    Software getDevices= new Software();
    getDevices.InfoGet();
    }

    public void Calculations(int devicesAmount){
    if (devicesAmount>=1000){
        NewDevices= devicesAmount - 1000;
    }
    else{
        NewDevices=devicesAmount;
    }
   }

  }
4

4 に答える 4

1

モバイルデバイスを使用しているときにユーザーとして投稿する理由はわかりませんが、要点は次のとおりです。

まず、クラスSoftwareには 2 つのオブジェクト "Device" があります。MVC アーキテクチャに従って、ビュー クラスに対して 1 つのコントローラー オブジェクトのみを宣言する必要があります。開始するには、このように Software クラスに対して 1 つの Device のみを宣言することをお勧めします。カプセル化を尊重するために、変数を非公開または保護する必要があるため、変数を保護するつもりです。

public class Software {

  protected Devices devices;
  protected Scanner softwareName;
  protected Scanner devicesAmount;
... }

Devices クラス変数についても同じことを行うことをお勧めします。

次に、クラスを介して値を移動できるように、Devices クラスの適切なコンストラクターを作成します。

public Devices (String softwareName) {
  this.softwareName = softwareName;
  this.newDevices = 0;
}

したがって、Devices クラスでこの値を持つことができます。次に、newDevicesのゲッターを追加し、toStringメソッドを Devices クラスに追加して、オブジェクトを出力できるようにする必要があります。

public int getNewDevices() {
  return newDevices;
}

public String toString() {
  return softwareName + " & " + newDevices;
}

次に、Software InfoGetメソッドでいくつかのものを移動して、デバイスを正しく構築し、それを印刷します。

public void InfoGet() {
String softwareName;
int devicesAmount;

Scanner sc= new Scanner(System.in);
System.out.println("Welcome to the Software License Calculator");
System.out.println("Please type in the name of the Software:");

softwareName = sc.nextLine();

System.out.println("");

System.out.println("Please type in the number of devices that have the software:");
devicesAmount=sc.nextInt();

devices = new Devices(softwareName); // So we initialize the object.
devices.Calculations(devicesAmount);

sc.close();
System.out.println(devices.toString()); // Here we just call the toString() method we build to print the values.
}

getNewDevices()メソッドは使用しませんでしたが、コントローラー クラスの getter と setter を構築することは、一般的に良い方法です ;)。

于 2015-12-12T10:09:05.897 に答える
-1

Calculations()(およびその他) はコンパイルされません。おそらく、そのメソッドに引数を追加する必要があり、完了です (?):

 public void Calculations(int devices2){
 ...
于 2015-12-12T09:08:49.140 に答える