如何在 ArrayLists 中使用多态性?
How to use polymorphism in ArrayLists?
所以我有这个动物园程序,我想在其中获得动物园中的房间列表,以及每个房间中的猫列表。
我有 3 个 classes:Felid
、Housecat
和 Wildcat
- Housecat
和 Wildcat
扩展 Felid
.根据动物的文字 class(现在我有老虎、波斯猫、暹罗猫和猎豹 - 老虎和猎豹扩展野猫,波斯猫和暹罗猫扩展家猫)某些属性将被自动分配。
Class 图 - http://i.imgur.com/VTSNRVA.jpg
例如,这些是猫科动物的字段:
String speciesName;
String furColour;
String gender;
int weightPounds;
boolean packAnimal;
String habitat;
int age;
这些是 housecat 的字段:
String ownerName;
String catName;
boolean feral;
这些是 wildcat 的字段:
boolean manEater;
在我的家猫构造函数中,我有
if(catName == null || catName.equals("")){
feral = true;
}
如果这只猫是野猫,当用户创建一个带有猫名的 HouseCat 并使用 'printCatInfo()':
@Override
public void printCatInfo(){
if(feral){
System.out.println("feral" + "\n" + speciesName + "\n" + furColour + "\n" + gender +
"\n" + weightPounds + "lbs\n" + "is not a pack animal" + "\n" + habitat + "\n" + age + " years old (human)");
}
else{
System.out.println("owner name is: " + ownerName + "\n" + "cat name is: " + catName + "\n" + speciesName + "\n" + furColour + "\n" + gender +
"\n" + weightPounds + "lbs\n" + "is not a pack animal" + "\n" + habitat + "\n" + age + " years old (human)" + "\n");
}
}
它不会打印它的名字。
feral
siamese
White or grey abdomen with black legs, face and tail
male
8lbs
is not a pack animal
urban
7 years old (human)
问题是它把所有的猫都算作野猫,我认为这是因为我用于猫列表的列表是:
ArrayList<Felid> catList = new ArrayList<Felid>();
所以我猜 catName 将始终为 null,因为添加到列表中的家猫只会算作类型 'felid'。
如何创建一个列表,我可以将所有的猫都扔进去,但仍将它们视为各自的 classes?
编辑:感谢您指出赋值运算符错误,尽管它仍然只打印 feral
最终编辑:非常感谢 'DoubleDouble' 向我指出如何使用 'Super()' - 这不是我所期望的问题所在。这是问题所在:
public class Siamese extends HouseCat{
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender){
this.speciesName = "siamese";
this.furColour = "White or grey abdomen with black legs, face and tail";
this.ownerName = ownerName;
this.catName = catName;
this.weightPounds = weightPounds;
this.age = age;
this.gender = gender;
}
}
新代码:
public class Siamese extends HouseCat{
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender){
super(catName);
this.speciesName = "siamese";
this.furColour = "White or grey abdomen with black legs, face and tail";
this.ownerName = ownerName;
this.catName = catName;
this.weightPounds = weightPounds;
this.age = age;
this.gender = gender;
}
}
if(feral = true){
这是赋值语句,不是比较语句。使用 ==
这一行:
if(feral = true){
您已将 true
分配给 feral
。您使用了赋值运算符 =
。比较运算符是==
。但是,feral
已经是一个 boolean
,因此您可以在条件中使用 feral
本身。
if (feral) {
你的构造函数似乎如下(如果我错了请纠正我):
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender)
{
this.weightPounds = weightPounds;
this.age = age;
this.ownerName = ownerName;
this.catName = catName;
this.gender = gender;
}
public HouseCat()
{
if(catName == null || catName.equals(""))
{
feral = true;
}
}
由于ownerName
、catName
、feral
都是HouseCat
class的一部分,所以最好让HouseCat
构造函数处理这些字段。
public HouseCat(String ownerName, String catName)
{
this.ownerName = ownerName;
this.catName = catName;
if(catName == null || catName.equals(""))
{
feral = true;
}
}
那时暹罗长这样:
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender)
{
super(ownerName, catName);
this.weightPounds = weightPounds;
this.age = age;
this.gender = gender;
}
所以我有这个动物园程序,我想在其中获得动物园中的房间列表,以及每个房间中的猫列表。
我有 3 个 classes:Felid
、Housecat
和 Wildcat
- Housecat
和 Wildcat
扩展 Felid
.根据动物的文字 class(现在我有老虎、波斯猫、暹罗猫和猎豹 - 老虎和猎豹扩展野猫,波斯猫和暹罗猫扩展家猫)某些属性将被自动分配。
Class 图 - http://i.imgur.com/VTSNRVA.jpg
例如,这些是猫科动物的字段:
String speciesName;
String furColour;
String gender;
int weightPounds;
boolean packAnimal;
String habitat;
int age;
这些是 housecat 的字段:
String ownerName;
String catName;
boolean feral;
这些是 wildcat 的字段:
boolean manEater;
在我的家猫构造函数中,我有
if(catName == null || catName.equals("")){
feral = true;
}
如果这只猫是野猫,当用户创建一个带有猫名的 HouseCat 并使用 'printCatInfo()':
@Override
public void printCatInfo(){
if(feral){
System.out.println("feral" + "\n" + speciesName + "\n" + furColour + "\n" + gender +
"\n" + weightPounds + "lbs\n" + "is not a pack animal" + "\n" + habitat + "\n" + age + " years old (human)");
}
else{
System.out.println("owner name is: " + ownerName + "\n" + "cat name is: " + catName + "\n" + speciesName + "\n" + furColour + "\n" + gender +
"\n" + weightPounds + "lbs\n" + "is not a pack animal" + "\n" + habitat + "\n" + age + " years old (human)" + "\n");
}
}
它不会打印它的名字。
feral
siamese
White or grey abdomen with black legs, face and tail
male
8lbs
is not a pack animal
urban
7 years old (human)
问题是它把所有的猫都算作野猫,我认为这是因为我用于猫列表的列表是:
ArrayList<Felid> catList = new ArrayList<Felid>();
所以我猜 catName 将始终为 null,因为添加到列表中的家猫只会算作类型 'felid'。
如何创建一个列表,我可以将所有的猫都扔进去,但仍将它们视为各自的 classes?
编辑:感谢您指出赋值运算符错误,尽管它仍然只打印 feral
最终编辑:非常感谢 'DoubleDouble' 向我指出如何使用 'Super()' - 这不是我所期望的问题所在。这是问题所在:
public class Siamese extends HouseCat{
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender){
this.speciesName = "siamese";
this.furColour = "White or grey abdomen with black legs, face and tail";
this.ownerName = ownerName;
this.catName = catName;
this.weightPounds = weightPounds;
this.age = age;
this.gender = gender;
}
}
新代码:
public class Siamese extends HouseCat{
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender){
super(catName);
this.speciesName = "siamese";
this.furColour = "White or grey abdomen with black legs, face and tail";
this.ownerName = ownerName;
this.catName = catName;
this.weightPounds = weightPounds;
this.age = age;
this.gender = gender;
}
}
if(feral = true){
这是赋值语句,不是比较语句。使用 ==
这一行:
if(feral = true){
您已将 true
分配给 feral
。您使用了赋值运算符 =
。比较运算符是==
。但是,feral
已经是一个 boolean
,因此您可以在条件中使用 feral
本身。
if (feral) {
你的构造函数似乎如下(如果我错了请纠正我):
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender)
{
this.weightPounds = weightPounds;
this.age = age;
this.ownerName = ownerName;
this.catName = catName;
this.gender = gender;
}
public HouseCat()
{
if(catName == null || catName.equals(""))
{
feral = true;
}
}
由于ownerName
、catName
、feral
都是HouseCat
class的一部分,所以最好让HouseCat
构造函数处理这些字段。
public HouseCat(String ownerName, String catName)
{
this.ownerName = ownerName;
this.catName = catName;
if(catName == null || catName.equals(""))
{
feral = true;
}
}
那时暹罗长这样:
public Siamese(int weightPounds, int age, String ownerName, String catName, String gender)
{
super(ownerName, catName);
this.weightPounds = weightPounds;
this.age = age;
this.gender = gender;
}