我创建了一个类,构造函数和访问器。我想知道是否有更简单的方法来做到这一点?
我有一个患者班:
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;
}
所以我可以创造新的患者:
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();
}
并通过菜单选项创建新患者:
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
我问了另一个问题For loop accepting an extra array member,然后意识到我觉得可能会为像我这样的新人做一些有用的Q&amp; A。
答案 0 :(得分:7)
这是错误的:
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 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;
}
在这里,您要使用参数值设置班级的字段。
我的意思是使用具有两个字段的类的简单示例:
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
+ "]";
}
}
请注意,上述代码中没有用户互动,没有。
还有一个单独的用户界面类,这里非常简单,主要方法都是:
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);
}
}
}
答案 1 :(得分:0)
我意识到我不需要getNewPt
。我可以这样创建new Patients
:
患者管理课程:
public static void main(String[] args) throws IOException
{
// creating an array of 5 patients
Patient patients[] = new Patient[5];
int i = 0;
for (i = 0; i < 5; i++)
{
patients[i] = new Patient(null, null, null, i, null, null);
}
Patient.getOption();
}
所以:
public static String getOption() throws IOException
{
System.out.println("bla bla");
option = stdin.readLine();
switch (option)
{
case ("N"):
new Patient (ptNo, ptName,
procDate, procType, injury, drName);
break; //and so on