我正在尝试从Scanner
为类对象赋值。我不想使用Teacher a = new Teacher("Ali","SCF333",'M');
,因为它已经设置好了,我希望用户输入数据
这是我的驱动程序类:
public class TeacherDriver {
public static void main(String[]args){
String data;
Teacher a = new Teacher();
Scanner input = new Scanner(System.in);
System.out.println("Please enter Employee Name: ");
data=input.nextLine();
a.setEmpName();
System.out.println("Please enter Employee ID: ");
data2=input.nextLine();
a.setEmpID();
System.out.println("Please enter Employee Gender: ");
data3=input.nextLine();
a.setGender();
System.out.println(a.toString());
}
}
这是班级:
public class Teacher {
private String empID;
private String empName;
private Address address;
private char gender;
private String [] Subject;
Teacher(){
}
Teacher(String empName){
this.empName=empName;
}
Teacher(String empName,String empID){
this.empName=empName;
this.empID=empID;
}
Teacher(String empName,String empID,char gender){
this.empName=empName;
this.empID=empID;
this.gender=gender;
}
Teacher(String empName,String empID,char gender,Address address){
this.empName=empName;
this.empID=empID;
this.gender=gender;
this.address=address;
}
Teacher(String empName,String empID,char gender,Address address,String[]Subject){
this.empName=empName;
this.empID=empID;
this.gender=gender;
this.address=address;
this.Subject=Subject;
}
public void setEmpID(){
this.empID=empID;
}
public void setEmpName(){
this.empName=empName;
}
public void setGender(){
this.gender=gender;
}
public String getEmpID(){
return empID;
}
public String getEmpName(){
return empName;
}
public char getGender(){
return gender;
}
public Address getAddress(){
return address;
}
public String[] getSubject(){
return Subject;
}
public String toString()
{
String viewteacher = new String("Name: " + empName + "\nID: "+ empID +"\nGender: "+ gender + "\nSubjects: "+ Subject);
return viewteacher;
}
}
答案 0 :(得分:0)
我不太确定你到底想要什么,但是你走了,
System.out.println("Please enter Employee Name: ");
data=input.nextLine();
a.setEmpName(data);
System.out.println("Please enter Employee ID: ");
data2=input.nextLine();
a.setEmpID(data2);
System.out.println("Please enter Employee Gender: ");
// Assuming that the user is entering correct data
data3=input.nextLine();
a.setGender(data3.charAt(0));
答案 1 :(得分:0)
这是创建setter方法的错误方法,这里只是将实例变量empName
设置为相同的实例变量empName
。
public void setEmpName(){
this.empName=empName;
}
你必须将参数传递给set方法,然后该参数才会设置。
public void setEmpName(String empName){
this.empName=empName;
}
TeacherDriver
类需要修改如下,
System.out.println("Please enter Employee Name: ");
data=input.nextLine();
a.setEmpName(data);
答案 2 :(得分:0)
获取输入后创建对象。
a.setEmpName();
a.setEmpID();
以上调用不会设置该值,因为没有任何参数传递给set。修改教师对象创建如下
Teacher a = new Teacher(data1,data2,data3);
将输入存储在data1,data2和data3变量中后,包括上述调用。
答案 3 :(得分:0)
您的setter没有传递任何参数来设置值。以下是他们通常应该看起来的方式
@CacheResult //how to specify the fixed key?
public SystemConfiguration getConfiguration() {
return configuration; //this is a prebuilt object, as described
}
然后你调用它们:
public void setSomething(String something) {
this.something=something;
}
您可以查看javadocs了解Scanner的工作原理