我的二传手和吸气者的问题

时间:2016-04-20 13:41:16

标签: java

我是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;
        }
    }
}

2 个答案:

答案 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;
    }
}
  1. 你忘了关闭构造函数的支撑。
  2. 在setNationality中你有这个国家=国籍;它将成员变量的国籍设置为自身。方法表示法应为public void setNationality(String nationality)
  3. 未使用listOfFriends,但这不一定是个问题。