打印对象的内容?

To print the content of object?

我应该在我的代码中执行哪些更改才能打印整个家庭 尝试过 toString,我只得到 null。这只是一个非常简单的代码,请帮助。

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Alpit on 26-05-2017.
 */
public class Fam {
    String father;
    String mother;
    String sister;
    String brother;
String r;

    public Fam(String father, String sister, String brother, String mother) {
        this.father = father;
        this.sister = sister;
        this.brother = brother;
        this.mother = mother;
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }


}
class add {
    public static void main(String args[]) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            String f = obj.readLine();
            String s = obj.readLine();
            String b = obj.readLine();
            String m = obj.readLine();
            Fam fam = new Fam(f, s, b, m);
            arrayList.add(fam);
        }
        for (Object x : arrayList) {

            System.out.println(String.valueOf(x));

        }
    }
}

我只得到对象的地址,这个问题可以被认为是 this question 的重复,但我无法通过那里提供的解决方案来理解。

这是我再次尝试的

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by Alpit on 26-05-2017.
 */
public class Fam {
    String father;
    String mother;
    String sister;
    String brother;
String r;

    public Fam(String father, String sister, String brother, String mother) {
        this.father = father;
        this.sister = sister;
        this.brother = brother;
        this.mother = mother;
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }

    public String toString()
    {
        return r;
    }


}
class add {
    public static void main(String args[]) throws IOException {
        BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
        ArrayList<Object> arrayList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            String f = obj.readLine();
            String s = obj.readLine();
            String b = obj.readLine();
            String m = obj.readLine();
            Fam fam = new Fam(f, s, b, m);
            arrayList.add(fam);
        }
        for (Object x : arrayList) {

            System.out.println(x.toString());

        }
    }
}

而这个 returns 为空。

您可以在 Class 家族中覆盖 Object 的 toString()。

您的 ArrayList 的类型参数是 Object。这很好,但是当您尝试在 for 循环中打印数据时,您需要将 x 转换为 Fam。像 ArrayList<Fam> arrayList = new ArrayList<>() 这样声明它会更容易。此外,您不能简单地按照您尝试的方式打印对象。对象引用保存内存地址的值。您需要使用 Fam class 中的 getter 方法手动打印数据。如果需要,您也可以覆盖 toString() 方法来执行此操作。

String.valueOf(object) 所做的是调用 class Object 的 toString() 方法(在您的例子中是 Fam)。

 public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();

}

因此您需要像这样覆盖 Fam class 中的 toString() 方法:

public class Fam {
String father;
String mother;
String sister;
String brother;
String r;
public Fam(String father, String sister, String brother, String mother) {
    this.father = father;
    this.sister = sister;
    this.brother = brother;
    this.mother = mother;
}

.
.
.

@Override
public String toString() {
    return this.father +" "+this.mother +" "+ this.sister +" "+ this.brother;
}

此外,您需要在主方法中进行此更改。

        ArrayList<Fam> arrayList = new ArrayList<Fam>();

这样你就可以打印你的对象了。 (PS:您可以在 toString() 方法中更改 return 格式。)

实现您自己的 toString() 方法。做这样的事情:

class Fam {
    String father;
    String mother;
    String sister;
    String brother;
    String r;

    public Fam(String father, String sister, String brother, String mother) {
        this.father = father;
        this.sister = sister;
        this.brother = brother;
        this.mother = mother;
    }

    public String getFather() {
        return father;
    }

    public void setFather(String father) {
        this.father = father;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(this.father);
        builder.append(" ");
        builder.append(this.sister);
        builder.append(" ");
        builder.append(this.brother);
        builder.append(" ");
        builder.append(this.mother);

        return builder.toString();
    }


} 

class add {
    public static void main(String args[]) throws IOException {
        Fam family = new Fam("father", "sister", "brother", "mother");

        System.out.println(family.toString());
    }
}

我在这里实现了 toString() 。我使用 StringBuilder 来演示如何创建自己的方法,在 "father, sister, brother, mother".

之间插入 space

运行 你得到:

father sister brother mother