只是通过 get/set 方法,我的输出有问题。它不显示对象的名字/姓氏,而是显示空/空。
任何人都可以提供任何见解,我不熟悉 get/set 方法。
代码
public class Person {
private String firstName;
private String lastName;
public Person (String a, String b){
a = firstName;
b = lastName;
}
public String getfirstName(){
return firstName;
}
public void setfirstName(){
this.firstName = firstName;
}
public String getlastName(){
return lastName;
}
public void setlastName(){
this.lastName = lastName;
}
public String toString() {
String s = "First name:" + firstName + "Last name:" + lastName;
return s;
}
}
这是我的类,只是为了创建对象并运行 toString 方法
public class PersonDriver {
public static void main(String[]args){
Person p1 = new Person ("Thomas", "Brown");
System.out.print(p1.toString());
}
}