我正在尝试使用accept方法将动物对象添加到宠物ArrayList中,但是我收到一条错误,指出找不到符号。我已经经历了很多次,我只是没有看到它。
感谢您的帮助。
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
public class Shelter2 implements AnimalShelter {
private ArrayList<Animal2> pet;
private int Id;
Scanner in = new Scanner(System.in);
public Shelter2() {
pet = new ArrayList<Animal2>();
SimpleDateFormat DateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date now = new Date();
String today = DateFormat.format(now).toString();
// pet.add(new Cat("Snow White", "Domestic Short Hair", "White", "F",
// "01-01-2012", null));
// pet.add(new Dog("Buster", "Beagle", "Brown/White/Black", "male",
// "12-25-2011", null));
// pet.add(new Reptile("Jack", "Lizard", "01-31-2012", null));
}
public String allAnimals() {
String str = "";
for (Animal2 p : pet) {
str = str + p + "\n\n";
}
return str;
}
public String available() {
String str = "";
for (int i = 0; i < pet.size(); i++) {
Animal2 p = pet.get(i);
if (p.getAdoptedDate() == null) {
str = str + p + "\n\n";
}
}
return str;
}
public String adopted() {
String str = "";
for (int i = 0; i < pet.size(); i++) {
Animal2 p = pet.get(i);
if (p.getAdoptedDate() != null) {
str = str + p + "\n\n";
}
}
return str;
}
public boolean adopt(int id) {
return true;
}
public boolean accept(Animal2 pet) {
String type = null;
System.out.println("What type of animal? (Cat, Dog, Reptile)");
type = in.next();
if (type == "Cat") {
System.out.println("Enter name: ");
String name = in.next();
System.out.println("Enter description: ");
String desc = in.next();
System.out.println("Enter color: ");
String color = in.next();
System.out.println("Enter sex: ");
String sex = in.next();
pet.add(new Cat(name, desc, color, sex, null, null));
}
return true;
}
}
public abstract class Animal2 {
public String name;
public String arrivalDate;
public String adoptedDate;
public String getName() {
return new String(name);
}
public void setName(String name) {
this.name = name;
}
public String getArrivalDate() {
return new String(arrivalDate);
}
public void setArrivalDate(String arrivalDate) {
this.arrivalDate = arrivalDate;
}
public String getAdoptedDate() {
return adoptedDate;
}
public void setAdoptedDate(String adoptedDate) {
this.adoptedDate = adoptedDate;
}
@Override
public String toString() {
return "\nName: " + name + "\nArrival Date: " + arrivalDate
+ "\nAdopted Date: " + adoptedDate;
}
}
class Cat extends Animal2 {
String desc;
String color;
String sex;
char s;
Cat(String name, String desc, String color, String sex, String arrivalDate,
String adoptedDate) {
super.setName(name);
super.setArrivalDate(arrivalDate);
super.setAdoptedDate(adoptedDate);
setDesc(desc);
setColor(color);
setSex(sex);
char s = ' ';
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
s = sex.toLowerCase().charAt(0);
if ((s == 'f') || (s == 'm')) {
this.sex = sex;
} else {
System.err.println("Illegal value for Cat sex field - " + sex);
}
}
@Override
public String toString() {
if (s == 'f') {
sex = "Female";
} else if (s == 'm') {
sex = "Male";
} else {
sex = null;
}
return "\nCat: " + super.toString() + "\nDescription: " + desc
+ "\nColor: " + color + "\nSex: " + sex;
}
}
class Dog extends Animal2 {
String bred;
String color;
String sex;
char s;
Dog(String name, String bred, String color, String sex, String arrivalDate,
String adoptedDate) {
super.setName(name);
super.setArrivalDate(arrivalDate);
super.setAdoptedDate(adoptedDate);
setBred(bred);
setColor(color);
setSex(sex);
char s = ' ';
}
public String getBred() {
return bred;
}
public void setBred(String bred) {
this.bred = bred;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
s = sex.toLowerCase().charAt(0);
if ((s == 'f') || (s == 'm')) {
this.sex = sex;
} else {
System.err.println("Illegal value for Dog sex field - " + sex);
}
}
@Override
public String toString() {
if (s == 'f') {
sex = "Female";
} else if (s == 'm') {
sex = "Male";
} else {
sex = null;
}
return "Dog: " + super.toString() + "\nBred: " + bred + "\nColor: "
+ color + "\nSex: " + sex;
}
}
class Reptile extends Animal2 {
String type;
Reptile(String name, String type, String arrivalDate, String adoptedDate) {
super.setName(name);
super.setArrivalDate(arrivalDate);
super.setAdoptedDate(adoptedDate);
setType(type);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Reptile: " + super.toString() + "\nType: " + type;
}
}
答案 0 :(得分:1)
我打算在这里走出去,说这条线给你错误:
pet.add(new Cat(name, desc, color, sex, null, null));
您收到此错误的原因是pet
方法范围内的accept
为Animal2
。如果要引用名为pet
的字段,请尝试:
this.pet.add(new Cat(name, desc, color, sex, null, null));
此外,在您的Dog
和Cat
课程中,您未设置s
。您正在创建新变量s
并将其设置为' '
(char s = ' ';
)。我不确定这会对你的项目有什么影响,但你可能不打算这样做。