简体   繁体   English

Java引用类

[英]Java referencing class from another

I have following two classes Animal and Zoo as below, 我有以下两类动物和动物园,

Animal Class 动物类

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;
    }
}

As you can see from my code that, I have tried to reference zoo class within Animal. 正如您从我的代码中看到的那样,我尝试在Animal中引用Zoo类。 And I have following method in Animal class, 我在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);
        }
    }
}

Above method should return an arraylist containing both animal and zoo, but at the moment it's only returning animal not zoo information. 上面的方法应该返回一个既包含动物又包含动物园的数组列表,但此刻它仅返回动物而不是动物园信息。 I have been going through stack overflow for class reference and stuff, but unable to figure out a clue. 我一直在通过堆栈溢出来获取类参考和内容,但无法找出线索。

FYI, my stored procedure works fine. 仅供参考,我的存储过程工作正常。

You want an ArrayList containing both animal and zoo? 您想要同时包含动物和动物园的ArrayList吗? 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: 看起来您只是将动物添加到了数组列表中,并且将动物园添加到了动物中,因此,如果要获取动物园,只需在Animal类中添加一个吸气剂即可:

public Zoo getZoo() {
    return _zoo;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM