创建不必要的吸气剂 [英] Creating an unnecessary getter
问题描述
我创建了一个类,构造函数和访问器.我想知道是否有更简单的方法来做到这一点?
I have created a class, constructors and accessors. I am wondering if there is an easier way to do this?
我有一个耐心的课程:
public Patient(final String ptNo, final String ptName,
final String procDate, final int procType, final String injury,
final String drName) throws IOException
{
Patient.ptNo = getPtNo();
Patient.ptName = getPtName();
Patient.procDate = getProcDate();
Patient.procType = getProcType();
Patient.injury = getPtNotes();
Patient.drName = getDrName();
}
也是该课程的学习者.
public static Patient getNewPt(String ptNo, String ptName,
String procDate, int procType, String
injury, String drName) throws IOException
{
Patient newPt = new Patient (ptNo,
ptName, procDate, procType, injury, drName);
return newPt;
}
所以我可以创建新的患者:
So I could create new patients:
public static void main(String[] args) throws IOException
{
Patient.getNewPt(null, null, null, 0, null, null);
// creating an array of 5 patients
Patient patients[] = new Patient[5];
int i = 0;
for (i = 0; i < 5; i++)
{
patients[i] = Patient.getNewPt(null, null, null, i, null, null);
}
Patient.getOption();
}
还可以通过菜单选项创建新患者:
And also create new patients through a menu option:
public static String getOption() throws IOException
{
System.out.println("bla bla");
option = stdin.readLine();
switch (option)
{
case ("N"):
Patient newPt = new Patient (ptNo,
ptName, procDate, procType, injury, drName);
break;// and so on
我问了另一个问题用于循环接受额外的数组成员,然后意识到我认为可能会对像我这样的新来者做出有益的问答.
I asked another question For loop accepting an extra array member and then realised something that I thought might make a helpful Q&A for newcomers like myself.
推荐答案
这是错误的:
public Patient(final String ptNo, final String ptName,
final String procDate, final int procType, final String injury,
final String drName) throws IOException
{
Patient.ptNo = getPtNo();
Patient.ptName = getPtName();
Patient.procDate = getProcDate();
Patient.procType = getProcType();
Patient.injury = getPtNotes();
Patient.drName = getDrName();
}
当您完全忽略作为参数传递的所有值时.而是:
As you're completely ignoring all values passed in as parameters. Instead do:
public Patient(final String ptNo, final String ptName,
final String procDate, final int procType, final String injury,
final String drName) throws IOException
{
Patient.ptNo = ptNo;
Patient.ptName = ptName;
Patient.procDate = procDate;
Patient.procType = procType;
Patient.injury = injury;
Patient.drName = drName;
}
您在此处使用参数值设置班级的字段.
Where here you're setting your class's fields with the parameter values.
使用具有两个字段的类的意思的简单示例:
A simple example of what I mean using a class with two fields:
Client.java
Client.java
public class Client {
private String clientNo;
private String clientName;
public Client(String clientNo, String clientName) {
this.clientNo = clientNo;
this.clientName = clientName;
}
public String getClientNo() {
return clientNo;
}
public void setClientNo(String clientNo) {
this.clientNo = clientNo;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
@Override
public String toString() {
return "Client [clientNo=" + clientNo + ", clientName=" + clientName
+ "]";
}
}
请注意,以上代码中没有用户交互,也没有.
Note that there is no user interaction in the above code, none.
还有一个用于用户界面的单独类,这里非常简单,并且全部在main方法中:
And a separate class for user interface, here quite simple and all in the main method:
ClientTest.java
ClientTest.java
import java.util.Scanner;
public class ClientTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Client[] clients = new Client[5];
for (int i = 0; i < clients.length; i++) {
System.out.print("Enter Client Number: ");
String clientNumber = scanner.nextLine();
System.out.print("Enter Client Name: ");
String name = scanner.nextLine();
clients[i] = new Client(clientNumber, name);
}
scanner.close();
for (Client client : clients) {
System.out.println(client);
}
}
}
这篇关于创建不必要的吸气剂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!