所以我有一堆用于动物医院的课程。我的超级类Pet,有四个常量,MALE,FEMALE,SPAYED,NEUTERED,它们被设置为数字。当读入数据文件时,在我的AnimalHospitalClass中,应该将性别设置为数字,返回Pet类将“sexId”与常量进行比较,然后返回性别。我所遇到的问题不符合逻辑但是技术性。出于某种原因,我的setter是(setSex(sexID))得到我的性别设置的数字,但当我尝试在我的getter中使用if语句来比较数字并返回性别时,sexID(这是一个Pet类中的变量)被设置回0,而不是我设置它。
继承我的宠物类:
public class Pet{
private String petName;
private String ownerName;
private String color;
protected int sexID;
public static final int MALE = 1;
public static final int FEMALE = 2;
public static final int SPAYED = 3;
public static final int NEUTERED = 4;
public Pet(String petName, String ownerName, String color){
this.petName = petName;
this.ownerName = ownerName;
this.color = color;
}
// getters
public String getPetName(){
return petName;
}
public String getOwnerName(){
return ownerName;
}
public String getColor(){
return color;
}
public String getSex(){
/* this is where I am having issues, instead of sexID being set to what
it is in the setter, it is set back to 0*/
System.out.println("SEXID: " + sexID); // will print 0
if(sexID == MALE){
return("male " );
}
else if(sexID == FEMALE){
return("female");
}
else if(sexID == SPAYED){
return("spayed");
}
else if(sexID == NEUTERED){
return("neutered");
}
else{ // will print else only since sexID is equal to 0
return("Not Available. " + sexID);
// in case there is no gender in the file
}
}
public void setSex(int sexID){
this.sexID = sexID;
System.out.println("SEX: " + sexID); // this will print the correct
// sexID that was set in the other class
}
public String toString(){
return(petName + "owned by " + ownerName
+ "\nColor: " + color
+"\nSex: " + getSex() );
}
}
这是我的动物医院类(因为它很大,只有一种方法):
import java.util.*;
import java.io.*;
public class AnimalHospital{
Scanner input;
private String pName;
private String pName2;
private String oName;
private String color ;
private String specialType; // store hair length for cats, size for dogs
private String gender;
private String type; // finds CAT, DOG, BIRD
public AnimalHospital(String inputFile)throws FileNotFoundException{
input = new Scanner(new File(inputFile));
}
public void printPetInfoByOwner(String name){
Pet pet = new Pet(pName,oName,color);
while(input.hasNext()){
String type = input.next(); // finds Cat, Dog, Bird
pName = input.next(); // gets pet name
oName = input.next(); // gets owner name
color = input.next();
gender = input.next();
if(gender.equals("male")){ // this is where I set the gender
int male = 1; // to a number so I can compare
pet.setSex(male);
}
if(gender.equals("female")){
int female = 2;
pet.setSex(female);
}
if(gender.equals("spayed")){
int spayed = 3;
pet.setSex(spayed);
}
if(gender.equals("neutered")){
int neutered = 4;
pet.setSex(neutered);
}
if(!(type.equals("BIRD"))){
specialType = input.next(); // since Bird does not have a special type
}
if(type.equals("CAT") && oName.equals(name)){
Cat cat = new Cat(pName, oName, color, specialType);
System.out.println(cat.toString());
break;
}
if(type.equals("DOG") && oName.equals(name)){
Dog d = new Dog(pName, oName, color, specialType);
System.out.println(d.toString());
break;
}
if(type.equals("BIRD") && oName.equals(name)){
Bird b = new Bird(pName, oName, color);
System.out.println(b.toString());
break;
}
}
}
这是一个示例输出:
CAT: Busker owned by Samantha
Color: Black
Sex: Not Available. 0 // reads sex as 0 should be 2, and female
Hair: short
AnimalHospital@55f96302 // also how do I get rid of this?
注意:
这是一项任务,我理解如果你不想给我答案,任何提示都会有所帮助,但我已经考虑了好几天而无法弄明白。
谢谢!
答案 0 :(得分:2)
您在pet
变量上设置了Pet的类型。然后你创造一只猫,鸟或狗,从不设置它的性别,并打印它。所以,既然你从未设定过Cat,Bird或Dog的性别,那就是0。