Merge remote-tracking branch 'origin/main' into main

This commit is contained in:
Jason Lu 2025-04-14 10:36:37 +08:00
commit b94f1254e0
3 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package feature;
public class Book {
private String title;
private String author;
private int pages;
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getPages() {
return pages;
}
@Override
public String toString() {
return "Book{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", pages=" + pages +
'}';
}
}

View File

@ -0,0 +1,15 @@
package feature;
/**
* create singleton using enum
*/
public class BookDemo {
public static void main(String[] args) {
Book book = new Book("Java Programming", "John Doe", 300);
System.out.println(book);
// Using the singleton pattern
BookEnum.INSTANCE.setBook(book);
System.out.println(BookEnum.INSTANCE.getBook());
}
}

View File

@ -0,0 +1,14 @@
package feature;
public enum BookEnum {
INSTANCE;
private Book book;
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}