1

我有培训对象编程,我坚持测试。我的程序扫描宠物信息并在屏幕上打印(基础知识)。当我输入宠物信息时:

name - Jimbo (min 3 max 16 letters)
type - 2 ( dog)
gender - 1 (male)

我得到了这个输出:

Pet info:
Your pet name is Jimbo, pet type is dog, and gender is male.

它写的一切正常,但是当我输入这个时:

name - Ji
type - 8 (out of choices)
gender - 8 (out of choices)

我得到了这个输出:

Pet info:
Your pet name is ji, pet type is unknown type, and gender is unknown gender.

而不是ji我应该得到的话"wrong name length",但它仍然打印ji。我很困惑。我已经开始 java 对象编程并且想完全理解它。

主.java

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner key_scan = new Scanner (System.in); 


        System.out.println("Enter your pet name(min 3 letters, max 16 letters): ");// select your pet name
        String pet_name = key_scan.nextLine();

        System.out.println("Enter your pet type number 1(cat), 2(dog), 3(mouse), 4(fish): ");// select your pet type
        int pet_type = key_scan.nextInt();

        System.out.println("Enter your pet gender 1(male), 2(female): ");// select your pet gender
        int pet_gender = key_scan.nextInt();

        petInfo obj_petInfo = new petInfo(pet_name, pet_type, pet_gender);

        System.out.printf("Pet info:\n%s", obj_petInfo.formatPetInfo());

        key_scan.close(); // close scanner key_scan
    }
}

宠物信息.java

public class petInfo {
    private String pet_name;
    private int pet_type;
    private int pet_gender;

    public petInfo(){
        this("Unknown", 0, 0);  
    }

    public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
        pet_name = pet_name_t;// string name
        pet_type = pet_type_t;// int type
        pet_gender = pet_gender_t;// int gender
    }
////////////////////////////////set methods 
    public void setPet_name(String pet_name){
        pet_name = ((pet_name.length() >= 3 && pet_name.length() <= 16)? pet_name : "wrong name length");
    }
///////////////////////////////// pet type  
    public void makePet_type_cat(int pet_type){
        pet_type = 1;
    }

    public void makePet_type_dog(int pet_type){
        pet_type = 2;
    }

    public void makePet_type_mouse(int pet_type){
        pet_type = 3;
    }

    public void makePet_type_fish(int pet_type){
        pet_type = 4;
    }
////////////////////////////// pet gender   
    public void makePet_gender_male(int pet_gender){
        pet_gender = 1;
    }

    public void makePet_gender_female(int pet_gender){
        pet_gender = 2;
    }

    boolean pet_is_male(){
        return this.pet_gender == 1;
    }

    boolean pet_is_female(){
        return this.pet_gender == 2;
    }

//////////////////////////////// pet type check
    boolean pet_is_cat(){
        return this.pet_type == 1;
    }

    boolean pet_is_dog(){
        return this.pet_type == 2;
    }

    boolean pet_is_mouse(){
        return this.pet_type == 3;
    }

    boolean pet_is_fish(){
        return this.pet_type == 4;
    }
/////////////////////////////////////get methods
    public String getPet_name(){
        return pet_name;
    }

    String getPet_type(){
    if (this.pet_is_cat())
            return "cat";
        else if(this.pet_is_dog())
            return "dog";
        else if(this.pet_is_mouse())
            return "mouse";
        else if(this.pet_is_fish())
            return "fish";
        else
            return "unknown type";  
    }   

    String getPet_gender(){
        if(this.pet_is_male())
            return "male";
        else if (this.pet_is_female())
            return "female";
        return "unknown gender";
    }

    String formatPetInfo(){
        return String.format("Your pet name is %s, pet type is %s, and gender is %s.", getPet_name(), this.getPet_type(), this.getPet_gender());
    }
}
4

3 回答 3

3

问题是,虽然setPetName对名称进行了验证,但构造函数在没有验证的情况下执行了“直接”赋值。

改变构造函数如下:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    setPet_name(pet_name_t);// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

这将解决问题。

请注意,您的代码违反了 Java 命名约定:setPet_name命名约定建议使用,而不是调用 setter 方法setPetName。遵循命名约定对于确保代码可读性非常重要,因此其他方法名称应以类似方式更改。

于 2013-07-14T17:14:01.153 回答
2

你还没有在任何地方调用你的 setPet_name,它基本上会进行验证。name 的值是通过以下构造函数调用分配的:

    petInfo obj_petInfo = new petInfo(pet_name, pet_type, pet_gender);

它调用下面的构造函数并在没有任何验证的情况下分配名称:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    pet_name = pet_name_t;// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}

您可以在构造函数中调用 setter 来分配值而不是直接分配:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    setPet_name(pet_name_t);// string name
    pet_type = pet_type_t;// int type
    pet_gender = pet_gender_t;// int gender
}
于 2013-07-14T17:14:17.653 回答
0

如果您想使用仍然可行的约定,Java 有一个this引用当前对象的关键字,因此您可以使用this如下:

public petInfo(String pet_name_t, int pet_type_t, int pet_gender_t){
    this.pet_name = pet_name_t;// pet_name_t refers to local variable get from parameterlist but this.pet_name_t refers to global variable of class what you defined as "private String pet_name;"
    this.pet_type = pet_type_t;// int type
    this.pet_gender = pet_gender_t;// int gender
}
于 2013-07-14T17:42:21.120 回答