【问题标题】:Storing an object within an object在对象中存储对象
【发布时间】:2011-12-06 21:13:24
【问题描述】:

自从我昨天发布关于从头开始构建链接列表的帖子以来,我已经取得了一些进展。

我遇到了一个新的障碍:在一个对象中存储一个对象。

假设我有一个具有以下属性的“书”类(忘记所有我熟悉的 set & get 方法):

 private String title;
 private int rating;

然后我将如何引用另一个类,例如“作者”,因为很明显一本书必须有一个作者,而且许多书可能有相同或不止一个作者。

这是我的“作者”类属性(再次忽略获取和设置):

 String authorName;
 String authorEmail;

我是否正确地认为我需要在“书”类中启动一个“作者”对象:

 private String title;
 private int rating; //mine
 private Author author = new Author();

我是否必须在每次创建“book”的新实例时设置属性 authorName 和 authorEmail?

非常感谢您的建设性反馈。

【问题讨论】:

    标签: java object


    【解决方案1】:

    您不一定需要在声明属性的地方实例化 Author 对象。我建议将已经实例化的 Author 传递给 Book 类的构造函数或 setter。然后,您可以将相同的作者传递给您创建的应该与之关联的每一本书。

    编辑:添加了一些代码 sn-ps:

    例如,如果你的 Book 构造函数是这样的:

    public Book(String title, int rating, Author author) {
        // set this.title, this.rating, and this.author to the passed-in parameters...
    }
    

    然后你会在这样的代码中调用它:

    Author bob = new Author(); 
    // You can set the name and email of the Author here using setters,
    //   or add them as args in the Author constructor
    Book firstBook = new Book("The First Book", 1, bob);
    Book secondBook = new Book("The Second Book", 2, bob);
    

    【讨论】:

    • 好答案。提供一个小代码 sn-p 作为示例会有很长的路要走。
    • 谢谢 - 根据建议添加了一些 sn-ps。
    【解决方案2】:

    这是多对多的关系。你需要你的 Author 类只是一个人和一本书之间的链接。然后事情就会解决。

    【讨论】:

      【解决方案3】:

      您可能需要某种单例作者列表,以防止同一作者出现多个副本。要么这样,要么你肯定需要重写 author 的 equals 方法。

      如果您使用单例,您可以在 AuthorList 对象中有一个 getAuthor 例程,该例程可以在作者不存在时创建作者,或者获取已创建的作者。

      【讨论】:

      • 我上次听说单例是一种反模式
      • 真的吗?怎么会?如果你想确保你不会意外地得到你不应该得到的多个副本,你会怎么做?它在我最近阅读的设计模式一书中,我发现它最有用。
      【解决方案4】:

      你在正确的轨道上。您可以使用ArrayList 来动态添加新作者,而无需调整任何内容。让我为你澄清一下:

      class Book {
          private String title;
          private int rating;
          private List<Author>authors = new ArrayList<Author>();
      
      
          public Book(String title, int rating, Author author) {
              this.title = title;
              this.rating = rating;
              authors.add(author);
          }
      
          public Book(String title, int rating, Author author) {
              this.title = title;
              this.rating = rating;
              this.author = author;
          }
      
          public void addAuthor(Author a) {
              authors.add(a);
          }
      
          public int numberOfAuthors() {return authors.size();}
      }
      class Author {
          private String name;
          private String email;
      
          public Author(String name, String email) {
              //...Same thing
          }
      }
      class Main {
          public static void main(String[] args) {
              Book book = new Book("Java Programming", 5, new Author("Me", "me@gmail.com"));
              Author contributingAuthor = new Author("My Friend", "cupcakes@gmail.com");
              book.addAuthor(contributingAuthor);
          }
      }
      

      【讨论】:

      • 这个答案不允许一本书有多个作者。
      猜你喜欢
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多