Java 如何从不同的类中获取变量?
所以我在做这个项目,我很难将变量从一个类移动到另一个类。这个微型软件的目标是让用户输入两条信息,一条是字符串,另一条是int。我还有两个类,一个用于检索信息,另一个用于计算信息。 这是第一类软件:Java 如何从不同的类中获取变量?,java,class,variables,java.util.scanner,external-methods,Java,Class,Variables,Java.util.scanner,External Methods,所以我在做这个项目,我很难将变量从一个类移动到另一个类。这个微型软件的目标是让用户输入两条信息,一条是字符串,另一条是int。我还有两个类,一个用于检索信息,另一个用于计算信息。 这是第一类软件: import java.util.Scanner; public class Software { public Scanner softwareName; public Scanner devicesAmount; public Software() { devicesAmount =
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);
}
}
这是第二类设备:
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;
}
}
}
计算()
(和其他)将无法编译。您可能需要向该方法添加一个参数,然后就完成了(?):
如果您想以“标准”的方式构建项目,我强烈建议您阅读MVC(model view controller) 为了解决您的问题,您可以向每个类添加需要移动的每个变量的getter和setter,以便在类之间移动值 对二传手来说是这样的:
public void setValue(Object value) {
this.value = value;
}
这是给getter的:
public Object getValue() {
return this.value;
}
扫描仪、设备、软件、类和具有相同名称的对象(大写或小写)之间非常容易混淆 我建议您清楚地重命名类和变量 一些进一步的建议:
- 为什么要将Scanner保留为变量?这不是数据,而是获取数据的媒介
- 为什么要记录数据?比如软件名(扫描器和字符串)
- 添加两个变量(softwarename和amount),私有
- 留着
- 添加要设置和获取的方法
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添加getter,并将toString方法添加到Devices类中,以便您可以打印对象
public int getNewDevices() {
return newDevices;
}
public String toString() {
return softwareName + " & " + newDevices;
}
然后我们将在软件InfoGet方法中移动一些东西,以正确构建de设备并打印它
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通常是一个很好的实践;) 它不起作用,我只得到一个输出:欢迎使用软件许可证计算器请键入软件名称:Microsoft请键入拥有该软件的设备数量:1500 Microsoft&Devices@5428c92fI不知道为什么"微软,Devices@5428c92f"出现了:
System.out.println(软件+“&”+findNumber)代码>我想我打错了:`findDevices.Calculations(设备2);findNumber.getNewDevices();sc.close();System.out.println(软件+“&”+findNumber);}public Object getValue(){return this.value;}`在Devices类中,我创建了setter,并将NewDevices初始化为value。有什么方法可以直接与您联系吗?让我们在这里尝试解决它,给我一点时间,我正在尝试给您,您是否应该放置每个setter或getter以使其工作;)修复了它们,希望这将有助于devicesAmount变量没有发送到Devices类,因为当我在编译时到达末尾时。。它打印出了软件名和数字0,尽管我输入了1500对不起!我在线路上的错误:devices.Calculations();,您应该将devicesAmount作为参数发送,如下所示:devices.Calculations(devicesAmount);美好的很高兴能帮助你!我想添加另一个类,叫做Prices,这次它只影响Devices类。。你介意帮我吗?有没有办法让我直接联系你?
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.
}