我修复了关于计算声明和输入变量的java代码。
public class JavaApplication1 {
/**
* @param args the command line arguments
*/
static Scanner sc = new Scanner(System.in);
static double maxLoad = 500;
static double currLoad;
static double loadInput = 0;
public static void main(String[] args) {
String cpNumber;
System.out.print("Enter Cellphone Number: ");
cpNumber = sc.nextLine();
// System.out.println();
System.out.print("Enter load to be bought: ");
loadInput = sc.nextDouble();
currLoad = computeLoad(maxLoad, loadInput);
System.out.println(loadInput + " was loaded to " + cpNumber);
System.out.println("Current Load Wallet is now only : " + currLoad);
}
public static double computeLoad(double x, double y) {
return x - y;
}
}
我只想征求专业人士的意见,如何改进我的代码编写以及如何添加setter getter方法,就像输入值不正确时返回一些验证一样。
答案 0 :(得分:7)
因此,让我们分析一下代码的优缺点。
缺点。
maxLoad
)声明为final
变量。getters
和setters
来检索它们。x
方法中有参数y
和参数computeLoad
。什么是x
和y
?这是不明确的命名,您应该不惜一切代价避免使用x
和y
(当然,除非您使用坐标)。因此,经过一些考虑后,我将这种想法应用于您的代码并进行了以下更改:
public class Wallet {
// The scanner used to read input data.
private Scanner sc;
// The maximum load allowed in the wallet.
private final double MAX_LOAD = 500;
// The current load in the wallet.
private double currLoad;
// The current input from the user.
private double loadInput;
/** This is the constructor. **/
public JavaApplication1() {
sc = new Scanner(System.in);
currLoad = 0.0;
loadInput = 0.0;
readInput();
}
/**
* Reads input from the user.
*
*/
public void readInput() {
System.out.print("Enter Cellphone Number: ");
cpNumber = sc.nextLine();
System.out.print("Enter load to be bought: ");
loadInput = sc.nextDouble();
// Perform calculation using input.
currLoad = computeLoad(maxLoad, loadInput);
System.out.println(loadInput + " was loaded to " + cpNumber);
System.out.println("Current Load Wallet is now only : " + currLoad);
}
/**
* Performs computation on the input.
* @param maxLoad the first value.
* @param loadInput the second value.
**/
private double computeLoad(double maxLoad, double loadInput) {
return maxLoad - loadInput;
}
/** The main thread. Used to create a new instance of JavaApplication1.
*
* This is so we don't need to litter static all over the place.
*/
public static void main(String[] args) {
new Wallet();
}
}
Getters and Setters
使用闪亮的新代码,可能是时候添加一些getter和setter了。这些具有非常明确的命名约定,您必须遵循 100%。我给你举个例子。假设您想为currLoad
值编写一个getter。然后它会是这样的:
/**
* Returns the current load.
* @return the current wallet load.
*/
public double getCurrLoad()
{
return currLoad;
}
查看如何使用get
预先填写,并且所有后续字词都以大写字母开头?下一个是制定者。
/**
* Updates the value in currLoad. Sets a new current wallet load.
*
* @param currLoad the new current wallet load.
*/
public void setCurrLoad(double currLoad)
{
this.currLoad = currLoad;
}
答案 1 :(得分:4)
当在单个(主)类中构建应用程序时,拥有getter和setter并没有多大意义。当您尝试在某些类上应用封装并应用 Java bean 原则时,Getter和setter是有意义的。尝试查找突出显示的关键字。
主类不是一个封装就绪的类。它是Java代码执行的入口点。如果您的应用程序变得更复杂,您最终将通过操纵更多不同类的对象来实现。然后定义getter和setter是有意义的。
此外,getter和setter用于公开实例字段,而不是静态变量。
答案 2 :(得分:2)
public class JavaApplication1 {`
这应该意味着什么,确定阶级的目的。
/**
* @param args the command line arguments
*/
static Scanner sc = new Scanner(System.in);
您已移动方法main的注释并已分配给sc
成员
static double maxLoad = 500;
static double currLoad;
static double loadInput = 0;
您分配的字段不一定是类成员
public static void main(String[] args) {
您已在main方法中实现了您的逻辑,该方法负责应用程序初始化并运行
String cpNumber;
您已声明该名称含糊不清
System.out.print("Enter Cellphone Number: ");
cpNumber = sc.nextLine();
System.out.println();
你留下了一个不必要的评论,即死代码。
currLoad = computeLoad(maxLoad, loadInput);
您使用之前声明的字段,应在此处声明。
System.out.println(loadInput + " was loaded to " + cpNumber);
System.out.println("Current Load Wallet is now only : " + currLoad);
您应该使用字符串格式。
对于第一个代码并不是那么糟糕。但是有一个改进的地方。
不要应用一些简单的规则。
示例:
public class MyFirstProgram {
private static double MAXIMUM_LOAD = 500.0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // We will read from the console.
System.out.print("Enter Cellphone Number: ");
String cellPhoneNumber = sc.nextLine();
System.out.print("Enter load to be bought: ");
double inputLoad = sc.nextDouble();
double computedLoad = computeLoad(maxLoad, loadInput);
System.out.println("%1$.2f was loaded to %s", inputLoad, cellPhoneNumber);
System.out.println("Current Load Wallet is now only : %1$.2f" computedLoad );
}
public static double computeLoad(double x, double y) {
return x - y;
}
}
从这里我们可以进一步提高,但为您提供指导。尝试使用这些树规则来创建可读代码。
下一步是将结构与控制器分开。
我们的结构:
public class Load {
private final String cellPhoneNumber;
private double load;
public Load(String cellPhoneNUmber) {
this.cellPhoneNumber = cellPhoneNumber
}
public void setLoad(double load) {
this.load = load;
}
public doulbe getLoad() {
return this.load;
}
public String toString() {
return String.format("%1$.2f is loaded to %s", getLoad(), cellPhoneNumber);
}
}
我们的逻辑:
public class MyFirstProgram {
private static double MAXIMUM_LOAD = 500.0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); //We will read from the console.
System.out.print("Enter Cellphone Number: ");
Load load = new Load(sc.nextLine());
System.out.print("Enter load to be bought: ");
load.setLoad(sc.nextDouble());
System.out.println(load);
computeLoad(load);
System.out.println(load);
}
public static double computeLoad(Load load) {
return load.setLoad(MAXIMIM_LOAD - load.getLoad());
}
}
下一个改进是分离责任,为此我们创建新方法
public class MyFirstProgram {
private static double MAXIMUM_LOAD = 500.0;
public static void main(String[] args) {
Load load = createLoad();
System.out.println(load);
computeLoad(load);
System.out.println(load);
}
public static double computeLoad(Load load) {
return load.setLoad(MAXIMIM_LOAD - load.getLoad());
}
public static Load createLoad() {
Scanner sc = new Scanner(System.in); //We will read from the console.
System.out.print("Enter Cellphone Number: ");
Load load = new Load(sc.nextLine());
System.out.print("Enter load to be bought: ");
load.setLoad(sc.nextDouble());
return load;
}
}
但是我们应该为它创建一个类来将它与主静态方法分开。
public class LoadManager {
public Load createNewLoad() {
private static final double MAXIMUM_LOAD = 500.0;
Scanner sc = new Scanner(System.in); //We will read from the console.
System.out.print("Enter Cellphone Number: ");
Load load = new Load(sc.nextLine());
System.out.print("Enter load to be bought: ");
load.setLoad(sc.nextDouble());
return load;
}
public computeLoad(Load load) {
return load.setLoad(MAXIMIM_LOAD - load.getLoad());
}
}
然后我们有:
public class MyFirstProgram {
public static void main(String[] args) {
LoadManger manager = new LoadManager();
Load load = manager.createLoad();
System.out.println(load);
manager.computeLoad(load);
System.out.println(load);
}
}
还有一件事要做,就是我们使用System.in
时的安全性,我们应该关闭这些资源。
public class LoadManager {
private static final double MAXIMUM_LOAD = 500.0;
public Load createNewLoad() {
Scanner scanner;
Load load ;
try {
scanner = new Scanner(System.in); //We will read from the console.
System.out.print("Enter Cellphone Number: ");
load = new Load(sc.nextLine());
System.out.print("Enter load to be bought: ");
load.setLoad(sc.nextDouble());
} finally {
if(scanner!=null)
scanner.close();
}
return load;
}
public computeLoad(Load load) {
return load.setLoad(MAXIMIM_LOAD - load.getLoad());
}
}