Can someone explain how list returns output from getOutputByBookType() method? Or main part with adding objects to books list?
I've solved this task with step by step method, but I'm not sure about whole code overall in terms of working.
Rozwiązane
Komentarze (5)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Guadalupe Gagnon
9 kwietnia 2021, 18:31
System.out.println() calls the toString() method on any object passed to it. The toString() method in the book class returns getOutputByBookType()
0
Guadalupe Gagnon
9 kwietnia 2021, 18:38rozwiązanie
small example:
no toString() method is called in the above code, but it still outputs the implementation of toString() for each object in the ArrayList. +3
Igor Karlik
10 kwietnia 2021, 00:32
That's exactly what I couldn't understand at first, thank a lot :)
0
Gellert Varga
8 kwietnia 2021, 21:32
For example, in that case if the Book is type of AgathaChristieBook, then the returned value will come from this line:
String agathaChristieOutput = author + ": " + getBook().getTitle() + " is a detective";
1.) 2.) 3.)
1.) 'author' is an instance-variable of the current Book-instance. You can write it this way too:
this.author
but it's enough if you write this way:
author
because of here we are inside an instance method, so the mentioned variable in the code basically means the variable of the current object, even without the 'this' keyword.
2.) getBook() method: you can look at this for example inside the class AgathaChristieBook:
public AgathaChristieBook getBook() {
return this;
}
So the getBook() returns the current object! So: getBook() == 'this' keyword!! (== 'this' object.)
So: getBook().getTitle() == this.getTitle()
But as i mentioned it above, here you don't need use the 'this' keyword:
getBook().getTitle() == this.getTitle() == getTitle() // try out the propgram with this code in your IDE-compiler.
3.) getTitle(): it's a getter method what returns to you the title variable of the class AgathaChristieBook.
+1
Igor Karlik
9 kwietnia 2021, 17:40
Sorry, I should've explain more specifically, my question is how method getOutputByBookType() is called in the main method. I see only its declaration in Books class :/
0