如何在阵列清单中使用多态性



所以我有这个动物园程序,我想在动物园中有一个房间列表,每个房间中的猫列表。

我有3个类:FelidHousecatWildcat -HousecatWildcat扩展Felid。取决于动物的字面类别(现在我有老虎,波斯语,暹罗和猎豹 - 老虎和猎豹扩展了野猫,波斯语和暹罗延伸范围)某些属性将自动分配。

课程图-https://i.stack.imgur.com/odydn.jpg

例如,这些是FELID的字段:

String speciesName;
String furColour;
String gender;
int weightPounds;
boolean packAnimal;
String habitat;
int age;

这些是housecat的字段:

String ownerName;
String catName;
boolean feral;

这些是野猫的领域:

boolean manEater;

在我的housecat构造函数中,我有

    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 + "lbsn" + "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 + "lbsn" + "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,因为添加到列表中的housecats只会算作'felid'类型。

如何创建一个可以将所有猫扔进的清单,但仍然将它们视为各自的课程?

编辑:感谢您指出分配操作员错误,它仍然只是打印野性

最终编辑:非常感谢您" doubleduble"指出如何将" 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;
    }
}

由于ownerNamecatNameferal都是HouseCat类的一部分,因此最好让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;
}

相关内容

  • 没有找到相关文章

最新更新