why would I have "the method is undefined for the type string" in java with this statement:
if(book.getTitle().getAuthor().getAward()){..}
the error is for getAward()
when I have defined a class Author as this:
class Author {
private String name;
private boolean award;
public Author(String n, boolean p){
name=n;
award=p;
}
public String getName(){return name;}
public boolean getAward(){return award;}
} May the error come from another definition (other than Author?)
change your getAuthor()
method implementation to that
public Author getAuthor(){
return new Author(); // or some author that is set in your object
}
And on that you can performe another method. if(book.getTitle().getAuthor().getAward())
if Author class have method getAward
public class Author{
public boolean getAward(){ // implementation
}
}
You need to change your getAuthor
method to
public Author getAuthor(){
return author;
}
and if you want the authors
award
, then u should invoke
book.getAuthor().getAward()
actually get Author is in another class and returns a string. getTitle is from a third class and returns string as well. Book is from a fourth class.
Then the answer is obvious.
You are attempting to call getAuthor()
on the result of calling getTitle()
. But the result of getTitle()
is a String
... and String
doesn't have a getAuthor()
method.
And that is exactly what the compilation error is telling you.
In fact, you probably should write:
if(book.getAuthor().getAward()){..}
then change getAuthor()
to return the Auteur
object ... rather than the author's name.
A book (Oevre) has an author, but a title (String) does not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.