在不同的类中打印多个对象

时间:2017-11-21 21:16:25

标签: java method-overriding

这个例子中的细节并不重要,只是想弄清楚如何解决这个问题,我有3个单独的类Person,Interests和Location。所有都是对象,一个人会有一个兴趣列表,每个兴趣都有一个位置列表,我使用toString来打印我的人物对象和兴趣,但我无法弄清楚如何打印了解每个兴趣点的位置。我需要重载我的toString吗?

public class Person{
    private String name;
    private ArrayList<Interest> interests = new ArrayList<Interest>();

    public Person(String name, ArrayList<Interest> interests) {
        this.name = name;
        this.interests = interests;
    }   

    public void addInterest(Interest newInterest) {
        interests.add(newInterest);
    }

    public Interest getInterest(int indexOfInterest) {
        return interests.get(indexOfInterest);
    }

    public ArrayList<Interest> getInterests() {
        return interests;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        String result = getName() + " ";
        for(Interest interest : interests) {
            result += interest.getName() + "(" + interest.getDangerRating() + ")" + " ";
        }
        return result.trim();
    }
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///

public class Interest {
        private int dangerRating;
        private String name;
        private ArrayList<Location> location = new ArrayList<Location>();

    public Interest (int dangerRating, String name, ArrayList<Location> location) {
        this.dangerRating = dangerRating;
        this.name = name;
        this.location = location;
    }
    public int getDangerRating() {
        return dangerRating;
    }
    public String getName() {
        return name;
    }
}

/////////////////////////////////////////////// /////////////////

public class Location {
    private String location;
    public Location (String location){
        this.location = location;
    }
    public String getLocation() {
        return location;
    }
}

3 个答案:

答案 0 :(得分:0)

学习使用您的工具:每个IDE都可以生成覆盖toString()方法,并且它们适用于大多数情况。

public class Location {
    // ...
    @Override
    public String toString() {
        return "Location{" +
                "location='" + location + '\'' +
                '}';
    }
}

public class Interest {
    // ...
    @Override
    public String toString() {
        return "Interest{" +
                "dangerRating=" + dangerRating +
                ", name='" + name + '\'' +
                ", location=" + location +
                '}';
    }
}

class Person{
    // ...
    @Override
    public String toString() {
        return "Person{" +
              "name='" + name + '\'' +
              ", interests=" + interests +
              '}';
    }
}

我建议您坚持使用IDE的默认toString(),因为这样可以使您的toString()看起来与您的所有项目保持一致,很快就会对您熟悉并且很容易读。

答案 1 :(得分:0)

我建议为每个班级制作一个toString。这样你可以在Intrest的toString中调用Location toString,然后在Person中调用toString的位置。此外,ArrayList类有自己的toString,用于打印数组中的所有项目。

目前你只有一个toString,你可以手动获取每个数据。例如:

(在位置类中):

@Override
public String toString()
{
    return "Location: " + location;
}

(兴趣类别):

@Override
public String toString()
{
     return "Danger Rating: " + dangerRating +
            "Name: " + name +
            "Location: " location.toString(); //Note, the name location is confusing here since it is an ARRAYLIST of locations.

}

(在人类中):

@Override
public String toString() 
{
    return "Name: " + name +
           "Intrests: " + intrests.toString();
}

(在主类):

public static void main(String[] args)
{
     Intrest[] intrests = new Intrest[4]; //TODO: Create intrests, currently they are all null.
     Person thePerson = new Person("Eddie", intrests);

     System.out.println("Person Info: " + thePerson.toString());
}

答案 2 :(得分:0)

在Person类中,您可以在所有兴趣的循环中调用interest.toString()

public String toString() {
    String result = getName() + " ";
    for(Interest interest : interests) {
        // add this function to print interest stuff
        result += interest.toString(); 
    }
    return result.trim();
}

在您的兴趣类中,您需要一个toString或类似的方法

public String toString(){
    String s = "";
    //print stuff for interest i.e. s+= what you want to add
    //loop through locations associated with Interest
   for(Location l : this.location ){
       // print what you want in location.toString()
       // add this funciton to your Location class
       s += l.toString()
   }
   return s;
}

然后在你的主要你需要打电话     person.toString(),它将遍历并打印所有内容。