我是java的新手。我的代码有些问题。当我开始写setter和getter时,我遇到了问题。这两个错误是:
参数getName的非法修饰符;只允许决赛
和
Void方法无法返回值
这是我的代码:
public class Profile {
private String name;
private String dayOfBirth;
private String townOfResidence;
private String counrtyOfResidence;
private String nationality;
private String listOfInterests;
private Profile listOfFriends;
public Profile(String name, String dateOfBirth, String townOfResidence, String
counrtyOfResidence, String nationality, String listOfInterests,
Profile listOfFriends){
this.name = name;
this.dayOfBirth = dateOfBirth;
this.townOfResidence = townOfResidence;
this.counrtyOfResidence = counrtyOfResidence;
this.nationality = nationality;
this.listOfInterests = listOfInterests;
this.listOfFriends = listOfFriends;
public String getName(){
return name;
}
public String getDateOfBirth(){
return dateOfBirth;
}
public void setTownOfResidence(String town){
this.townOfResidence = town;
}
public String getTownOfResidence(){
return townOfResidence;
}
public void setNationality(String Nationality){
this.nationality = nationality;
}
public String getNationality(){
return nationality;
}
public void setlistOfInterests(String interests){
this.listOfInterests = interests;
}
public String getlistOfInterests(){
return listOfInterests;
}
public void setCounrtyOfResidence(String country){
this.counrtyOfResidence = country;
}
public String getCounrtyOfResidence(){
return counrtyOfResidence;
}
}
}
答案 0 :(得分:6)
如果您尝试格式化代码,可以注意到在关闭构造函数体之前已经引入了访问器方法(getter和setter):
public class Profile {
private String name;
private String dayOfBirth;
private String townOfResidence;
private String counrtyOfResidence;
private String nationality;
private String listOfInterests;
private Profile listOfFriends;
public Profile(String name, String dateOfBirth, String townOfResidence, String
counrtyOfResidence, String nationality, String listOfInterests
, Profile listOfFriends) {
this.name = name;
this.dayOfBirth = dateOfBirth;
this.townOfResidence = townOfResidence;
this.counrtyOfResidence = counrtyOfResidence;
this.nationality = nationality;
this.listOfInterests = listOfInterests;
this.listOfFriends = listOfFriends;
}
public String getName() {
return name;
}
public String getDayOfBirth() {
return dayOfBirth;
}
public void setTownOfResidence(String town) {
this.townOfResidence = town;
}
public String getTownOfResidence() {
return townOfResidence;
}
public void setNationality(String Nationality) {
this.nationality = nationality;
}
public String getNationality() {
return nationality;
}
public void setlistOfInterests(String interests) {
this.listOfInterests = interests;
}
public String getlistOfInterests() {
return listOfInterests;
}
public void setCounrtyOfResidence(String country) {
this.counrtyOfResidence = country;
}
public String getCounrtyOfResidence() {
return counrtyOfResidence;
}
}
答案 1 :(得分:0)
您的代码存在问题。 因为它会更容易,我会给你工作版本:
public class Profile {
private String name;
private String dayOfBirth;
private String townOfResidence;
private String counrtyOfResidence;
private String nationality;
private String listOfInterests;
private Profile listOfFriends;
public Profile(String name, String dateOfBirth, String townOfResidence, String counrtyOfResidence,
String nationality, String listOfInterests, Profile listOfFriends) {
this.name = name;
this.dayOfBirth = dateOfBirth;
this.townOfResidence = townOfResidence;
this.counrtyOfResidence = counrtyOfResidence;
this.nationality = nationality;
this.listOfInterests = listOfInterests;
this.listOfFriends = listOfFriends;
}
public String getName() {
return name;
}
public String getDateOfBirth() {
return dayOfBirth;
}
public void setTownOfResidence(String town) {
this.townOfResidence = town;
}
public String getTownOfResidence() {
return townOfResidence;
}
public void setNationality(String nationality) {
this.nationality = nationality;
}
public String getNationality() {
return nationality;
}
public void setlistOfInterests(String interests) {
this.listOfInterests = interests;
}
public String getlistOfInterests() {
return listOfInterests;
}
public void setCounrtyOfResidence(String country) {
this.counrtyOfResidence = country;
}
public String getCounrtyOfResidence() {
return counrtyOfResidence;
}
}
setNationality(String nationality)