在方法(Java)

时间:2016-11-06 21:58:53

标签: java

我有一个类LazyAuthor,它存储有关作者的信息,并且可以为它设置一个“竞争对手”(同一类的另一个对象),并且各种方法使它们相互交互。 LazyAuthor类还依赖于另一个类Book,它存储了书名的详细信息等。

我正在尝试设置一个方法rivalsLastTitle,它返回竞争对手最后一本书的标题(来自相应的Book对象)。我把它写成:

public String rivalsLastTitle(){
    return currentRival.getLastBook().getTitle();
}

我所称的相应方法是

public Book getLastBook(){
    return lastBook;
}

来自LazyAuthor类和

public String getTitle(){
    return title;
}
来自Book类的

。 currentRival是LazyAuthor类的一个对象。

但是这会返回一个空值,所以我的语法错误。我不知道如何编写这段代码,以便它返回竞争对手的lastBook对象的标题。我相信我可能会对return语句的指令过于复杂,但我之前已经看到过像这样的多个方法调用。

任何帮助将不胜感激,如果有用,我很乐意分享代码的其他部分。谢谢

编辑:这是整个LazyAuthor类的代码。道歉:

// fields (data members) go here
private int currentMoney, newMoney;
private String authorName, name;
private Book lastBook, bestBook, rLastBook, newBook, newLastBook,newBestBook, nextBook;
private LazyAuthor currentRival, newRival;
private boolean happy, jealous, lol;

// methods go here

public LazyAuthor(String name){
    authorName = name;
    currentMoney = 10000;
}

//set methods
public void setLastBook(Book newLastBook){
    lastBook = newLastBook; 
}

public void setBestBook(Book newBestBook){
    bestBook = newBestBook;
}

public void setRival(LazyAuthor newRival){
    newRival = currentRival;
}


public void setMoney(int newMoney){
    newMoney = currentMoney;
}

public void setName(String name){
    name = authorName;
}

//get methods
public Book getLastBook(){
    return lastBook;
}

public Book getBestBook(){
    return bestBook;
}

public int getMoney(){
    return currentMoney;
}

public String getName(){
    return authorName;
}

public LazyAuthor getRival(){
    return currentRival;
}

// complex methods

public String lastTitle(){
    return lastBook.getTitle();
}

public String bestTitle(){
    return bestBook.getTitle();
}

public String rivalsLastTitle(){
    rLastBook = currentRival.getLastBook();
    return rLastBook.getTitle();
}

public boolean isHappy(){
    // happy if last book written is their best OR they sold film rights for it
    if(lastBook == bestBook || lastBook.filmRightsAreSold() == true) {
        happy = true; }
    else { 
    happy = false; }

    return happy;
}

public boolean isJealous(){
    // jealous if their rival's last book made more money, but isn't jealous if they managed to sell film rights for their last book
    rLastBook = currentRival.getLastBook();
    if(rLastBook.getValue() > lastBook.getValue() ){
        jealous = true; }
    else {
        jealous = false;}
    if(lastBook == bestBook || lastBook.filmRightsAreSold() == true) {
        jealous = false; }


    return jealous;
}

public boolean laughsAboutRival(){
    // they laugh if their rival didn't sell film rights for their last book or if it made less than their own
    rLastBook = currentRival.getLastBook();
    if(rLastBook.filmRightsAreSold() == false || currentRival.getLastBook().getValue() < lastBook.getValue()) {
        lol = true; }
    else {
        lol = false;}

    return lol;
}

public void buyBackRightsOfLastBook(){
    if(lastBook.filmRightsAreSold() == true && currentMoney >= lastBook.getBuyBackCost() ){
        setMoney(currentMoney - lastBook.getValue() );
        lastBook.buyBackRights(); }

}

public void writeNewBook(Book newBook){
// writes new book only if their last book wasn't their best, and they   didn't sell the rights to it.
    if(lastBook!=bestBook && lastBook.filmRightsAreSold() == false && currentMoney < lastBook.getValue() ){
        lastBook = newBook;
        currentMoney= currentMoney + lastBook.getValue();
    }
}

编辑2:在编辑这篇文章时刚刚查看了代码,我似乎已经错误地分配了一些set方法中的标签。我现在正在编辑它,看看它是否修复了空指针。

编辑3:这似乎有效,谢谢大家保证我原来的语法是正确的,也是为了澄清空值意味着什么出错:)

0 个答案:

没有答案