【问题标题】:How to code methods in your classes如何在类中编写方法
【发布时间】:2013-11-08 17:16:23
【问题描述】:

我需要帮助来了解如何编写诸如“如果已设置,则执行此操作”之类的语句之类的方法。例如,我写了一个 Author 和一个 Book 类。在我的 Book 类中,我需要一个返回书名的方法。但是,如果已经设置了书的作者(即 Book 中有一个设置作者的方法),则返回书名加上作者姓名。

这是我为这些类编写的一些代码

public class Author{
  String authorFirstName;
  String authorLastName;
  int authorBirth;
  int authorDeath;

  //creates a new author object with nothing set
  public Author(){
  }

  //String l=last name, String f=frist name
  //creates new author object by setting the last and first name
  public Author(String l, String f){
    authorLastName=l;
    authroFirstName=f;
  }
//end of Author class
 } 

public class Book{
  int bookYearPublish;
  Author bookAuthor;
  String bookNumberISBN;
  String bookTitle;

  public Book(){
  }

  //String t is the title of the book
  //creats a Book object with a title
  public Book(String t){
    bookTitle=t;
  }

  //String t is the title of the book
  //Author a is the author of the book
  //creats a Book object with a title and author
  public Book(String t, Author a){
    bookTitle=t;
    bookAuthor=a;
  }

  //Author a is the author that will be set for the Book
  //sets the author of the book
  public void setAuthor(Author a){
    bookAuthor=a;
  }

  //returns the Author of the Book
  public Author getAuthor(){
    return bookAuthor;
  }

  //returns the title of Book
  //if the author is known returns a String in the form of title. last name, first name of author
  //if the year is known returns a String in the form title (year). last name, first name of author
  public String toString(){
    String title=bookTitle;
    if(bookAuthor.equals(this.getAuthor())){//I am getting a NullPointException here so this is where my problem is
      title=title+". "+bookAuthor;
    }
    if(bookYearPublish.equals(this.getYear())){
      title=bookTitle+" ("+bookYearPublish+"). "+bookAuthor;
    }
    return title;
  }

  //ends class Book
}

【问题讨论】:

  • 这是作业还是什么?如果没有,我建议你不要这样做。拥有一种基本上可以返回不同事物的方法是令人困惑的。您将不得不围绕该方法进行编码。它是只返回标题,还是返回标题和作者?当您调用该方法时,您不会知道,因此您必须对结果进行某种评估和/或解析。我会为单独的属性使用单独的 getter。
  • toString() 方法返回书名,除非已设置作者和/或出版年份。有标题、作者和出版年份的 setter 方法。因此,如果已设置作者,则返回“标题。作者。如果已设置出版物,则返回“标题(年份)。作者
  • 我明白你想要做什么,我只是不认为这是一个好主意。比如你已经设置了标题和作者,但是你只想要标题,你不能得到它。您将得到一个包含标题和作者的字符串,并且您必须解析该字符串。如果您希望在一个方法中返回所有这些属性,请让该方法返回一个字符串数组。

标签: class object methods comparison


【解决方案1】:

只需编写一个 getTitle() 方法(如 getAuthor 方法)就可以了

 public string getTitle()
{
  String s = this.title;
if(author != "" || author != null)
{
s+=this.author;
}
return s;
}

【讨论】:

  • 我的整个代码中确实有一个 getTitle() 方法,所以我会尝试格式化并查看它是否有效。感谢您的意见。
【解决方案2】:

代替:

if(bookAuthor.equals(this.getAuthor())){

if(bookAuthor != null && bookAuthor.equals(this.getAuthor())){

但是,我认为这不是您想要的...this.getAuthor() 返回 bookAuthor 所以它应该始终匹配...我认为您真正想要的是:

if( bookAuthor != null) {
    title=title+". "+bookAuthor;
}

【讨论】:

  • 好吧,我也试试这个表格。希望它有效,并感谢您的意见
  • 还有一个预设作者AUTHOR_NOT_SET=new Author("AUTHOR NOT SET, "");
  • 所以我是使用该作者而不是 null 还是使用 || 将其添加到条件中
  • 与其“特殊未设置”作者,不如考虑只添加ifauthor==null,输出特殊文本。
【解决方案3】:

在你的setAuthor(Author a) 中设置作者后,试着把你想做的动作像这样:

public void setAuthor(Author a){
bookAuthor=a;

// put the action here.
}

【讨论】:

    猜你喜欢
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    相关资源
    最近更新 更多