简体   繁体   English

如何将 object 的 class 具有不同 class 成员的 ZCC8D68C551C4A9A6D5313E07DE4DE

[英]How to add an object that its class has a different class member to an CSV?

Taking this example:举个例子:

public class Dog {
    private String name;
    private Owner owner;
}

public class Owner {
    private String name;
    private int age;
    private String address;
}
  • How I can add multiple Dog objects to a CSV?如何将多个 Dog 对象添加到 CSV?
  • How would they look like in the CSV with their owner object inside it?在 CSV 中,它们的所有者 object 会是什么样子?
  • How are Java Beans useful in this situation? Java Bean 在这种情况下有何用处?

There are a few approaches, depending on what you want to accomplish.有几种方法,具体取决于您要完成的任务。 You could write a function that writes a separate CSV for the owners and dogs, where they could reference each other using a key, like a sql primary key .您可以编写一个 function ,它为主人和狗编写一个单独的 CSV ,它们可以使用一个键相互引用,例如sql 主键 Or you could simply inline the information into one, maybe with columns like:或者您可以简单地将信息内联到一个中,可能使用如下列:

dog_name, owner_name, owner_age, owner_address

What Java Beans can allow you to do is what is called serialization, where you let Java handle how to read and write objects from disk. Java Beans 可以让你做的就是所谓的序列化,你让 Java 处理如何从磁盘读取和写入对象。 Normally, you'd run into issues where Java does not know how to serialize objects that are nested inside each other, but by using Beans you can still accomplish this.通常,您会遇到 Java 不知道如何序列化相互嵌套的对象的问题,但使用 Beans 您仍然可以完成此操作。 You could do this by making Dog implement java.io.Serializable .您可以通过使 Dog implement java.io.Serializable来做到这一点。 Check out this guide from TutorialsPoint.从 TutorialsPoint 查看本指南 More info about beans here .有关豆类的更多信息在这里

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

相关问题 如何从其成员变量中获取类对象? - How can I get the class object from its member variable? 如何将对象列表添加到另一个具有对象列表成员的 class? - How to add a list of objects to another class that has a member that is a list of objects? 给定类成员名称或对象,如何在Java中找到其声明/封闭类对象? - Given a class member name or object, how can I find its declaring/enclosing class object in java? 如何用自己的类型的成员扩展类? - How to extend a class with a member of its own type? 如何在不同的类中使用相同的对象及其值 - How to use same object with its value in different class java中每个用户定义的类都有一个与之关联的类Class对象吗? 然后它与反射有何不同? - Do every user defined Class in java has a class Class object associated with it ? Then how it is different from Reflection? Java:如何访问类层次结构中对象的成员,其中一个成员更改了其数据类型 - Java: How to access members of an object in a class hierarchy where one member changes its data type 如何使用Hibernate检索类的成员对象? - How to retrieve a member object of a class using Hibernate? 如何通过Class对象访问静态成员 - How to access a static member through a Class object 如何在Nashhorn JavaScript中扩展Java类并添加类成员变量 - How to extend Java class in Nashhorn JavaScript and add class member variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM