我接受过对象编程培训,并且坚持进行测试。
我的程序扫描宠物信息并在屏幕上打印(基本知识)。
当我输入宠物信息时:
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对象编程,并且想完全理解它。
Main.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
}
}
petInfo.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());
}
}
最佳答案
问题在于,尽管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
。遵循命名约定对于确保代码可读性非常重要,因此应以类似方式更改其他方法名称。