14-clases.java 813 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class Book {
  2. private String title;
  3. private String author;
  4. private int year;
  5. // Constructor.
  6. public Book(String title, String author, int year) {
  7. this.title = title;
  8. this.author = author;
  9. this.year = year;
  10. }
  11. public String getTitle() {
  12. return this.title;
  13. }
  14. public String getAuthor() {
  15. return this.author;
  16. }
  17. public int getYear() {
  18. return this.year;
  19. }
  20. @Override
  21. public String toString() {
  22. return "Book{" +
  23. "title='" + title + '\'' +
  24. ", author='" + author + '\'' +
  25. ", year=" + year + '}';
  26. }
  27. }
  28. class Main {
  29. public static void main(String[] args) {
  30. Book bookOne = new Book("Java", "John Doe", 2022);
  31. Book bookTwo = new Book("Python", "Jane Smith", 2021);
  32. System.out.println(bookOne);
  33. System.out.println(bookTwo);
  34. }
  35. }