gpt4 book ai didi

java - setter/getter 无法正常工作

转载 作者:行者123 更新时间:2023-11-30 06:02:50 25 4
gpt4 key购买 nike

我正在处理一个报告文件,以调用多种方法来对外部报告文件运行。下面是我的 report.java 文件,并且 getter 元素似乎不起作用。这些元素是generateReport 和isServerActive。这就是我试图弄清楚的,因为我的所有其他方法在调用时都可以正常工作,但这两个方法似乎仍然挂起。

class Report {
//Variables to be used within my methods/functions below
private static String serverName;
private static String userName;
private static String password;
private String reportName;
private int numParameters;
private int reportParameter;
private String reportParameterString;
private static String outputType;
private static String systemName;
private static String genReport;
private static boolean active;

// Constructor
Report(String name){
reportName = name;

}

//setServerName method, assigned variable serverName the value passed into "name"
public static void setServerName(String name){
serverName = name;
}

//setUserName method, assigned variable userName the value passed into "user"
public static void setUserName(String user){
userName = user;

}

//setPassword method, assigned variable password the value passed into "pw"
public static void setPassword(String pw){
password = pw;
}

//setNumParameters method, assigned variable numParameters the value
passed into int "numParm"
public void setNumParameters (int numParm){
numParameters = numParm;
}

//setParameter method, assigned variable reportParameter the value
passed into int "reportParam"
//Assigned variable reportParameterString the value passed into
"param"
public void setParameter (int reportParam, String param){
reportParameter = reportParam;
reportParameterString = param;

}

//setOutputType method, assigned variable outputType the value passed
into "output"
public void setOutputType (String output){
outputType = output;

}

//setReportSystemName method, assigned variable systemName the value
passed into "reportSystemName"
public void setReportSystemName (String reportSystemName){
systemName = reportSystemName;
}

public void generateReport(String reportGen){
genReport = reportGen;

}

public void isServerActive(boolean isActive){
active = isActive;

}


}

这是我调用上述所有方法的文件

public class ReportClassPrinter {
public static void main(String[] args) {


//Set the server name
Report.setServerName("\\\\fancyServer");
Report.setUserName("NHAUser");
Report.setPassword("NHAPassword");

//Create the two reports
Report report1 = new Report("Report #1");
Report report2 = new Report("Report #2");

//Set the numbe of parameters for each report
report1.setNumParameters(2);
report2.setNumParameters(4);

//Add the needed parameters, Report should make sure I am not trying to break it
report1.setParameter(0, "01/01/1970");
report1.setParameter(1, "01/01/2018");
report1.setParameter(2, "pjdt");

report2.setParameter(0, "08/01/2017");
report2.setParameter(1, "08/01/2018");
report2.setParameter(2, "notpjdt");
report2.setParameter(3, "THIS IS A PARAMETER");
report2.setParameter(4, "THIS WON'T BE ADDED");

//Set the output type
report1.setOutputType("pdf");
report2.setOutputType("xls");

//Set the report system name:
report1.setReportSystemName("reportNumberOne.rdl");
report2.setReportSystemName("reportNumberTwo.rdl");


//Display the Report information
System.out.println(report1.generateReport());
System.out.println("Server up is: " + Report.isServerActive());
System.out.println(report2.generateReport());
System.out.println("Server up is: " + Report.isServerActive());


//Change the server - notice how chaning this once, affects ALL reports
System.out.println("\nUpdating Server information\n");
Report.setServerName("\\\\SercureServerName");
Report.setUserName("SecureNHAUser");

//Again display the Report information
System.out.println(report1.generateReport());
System.out.println("Server up is: " + Report.isServerActive());
System.out.println(report2.generateReport());
System.out.println("Server up is: " + Report.isServerActive());


}

}

最佳答案

你需要

  1. getters 最后一行的 return 语句
  2. 更改函数的返回类型
  3. 从函数签名中删除参数,因为您不使用参数调用函数
  4. 将静态添加到返回类型

按照您的预期在 Main 功能上工作。您在 getter 中所做的只是为变量分配值。

class Report {
[...]
public static String generateReport(){
return genReport;
}

public static boolean isServerActive(){
return active;
}
}

关于java - setter/getter 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51922140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com