Java引用另一个类

时间:2016-04-15 14:30:32

标签: java oop

我有以下两个类Animal和Zoo,

动物类

public class Animal {
    private int id;
    private String name;
    private String  animal_order;
    private String family;
    private String  genus;
    private String  species;
    private int number_avilable;
    private Zoo _zoo;

    public Animal() {

    }

    public Animal(ArrayList<Animal> allAnimals) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Animal (int id, String name, String  animal_order, String family, String  genus, String  species,int number_avilable, Zoo _zoo){
        this.id = id;
        this.name = name;
        this.animal_order = animal_order;
        this.family = family;
        this.genus = genus;
        this.species = species;
        //this.zoo = zoo;
        this.number_avilable = number_avilable;
        this._zoo = _zoo;

    }
}

Zoo Class

public class Zoo {
    private String zoo_name;
    private String  link;
    private String lat;
    private String  lng;
    private String  postcode;
    private String image;

    public Zoo() {

    }

    public Zoo(ArrayList<Zoo> allZoos) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public Zoo (String zoo_name, String  link,String  postcode, String image){
        this.zoo_name = zoo_name;
        this.link = link;
        //this.lat = lat;
        //this.lng = lng;
        this.postcode = postcode;
        this.image = image;
    }
}

从我的代码中可以看出,我试图在Animal中引用zoo类。我在Animal类中有以下方法,

public static ArrayList<Animal> getAllAnimals() throws Exception {
    PreparedStatement query = null;
    Connection conn = null;

    ArrayList<Animal> results = new ArrayList();
    try {

        // connect to database
        conn = Database.mySqlDBConn().getConnection();
        // run a query
        query = conn.prepareStatement("{CALL get_animal()}");
        ResultSet rs = query.executeQuery();
        while (rs.next())
        {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            String  animal_order = rs.getString("animal_order");
            String family = rs.getString("family");
            String  genus = rs.getString("genus");
            String  species = rs.getString("species");
            int number_avilable= rs.getInt("number_avilable");
            String zoo_name = rs.getString("zoo_name");
            String link = rs.getString("link");
            String postcode = rs.getString("postcode");
            String image = rs.getString("image");
            Zoo _zoo = new Zoo(zoo_name, link, postcode, image);
            Animal animal = new Animal(id, name,animal_order,family, genus, species,number_avilable, _zoo);
            results.add(animal);
        }
    }
}

上面的方法应该返回一个包含动物和动物园的arraylist,但此刻它只返回动物而不是动物园信息。我一直在通过堆栈溢出来获取类参考和东西,但无法找出线索。

仅供参考,我的存储过程正常。

1 个答案:

答案 0 :(得分:1)

You want an ArrayList containing both animal and zoo? It looks like your only adding the animal to the arraylist, and adding the zoo to the animal, so if you want to get the zoo, just add a getter in the Animal class:

public Zoo getZoo() {
    return _zoo;
}