Bookpack:
The example creates a package called bookpack, which contains a simple class for managing a database of books.
Book Class:
It has private attributes title, author and pubDate (title, author and publication date).
Constructor method initializes attributes.
show() method displays the book details.
BookDemo Class:
Creates an array of 5 Book objects.
Populates the array with book information and displays the details using the show() method.
Code Example
Directory Structure:
src/ bookpack/ BookDemo.java
// Demonstração breve dos pacotes. package bookpack; class Book { private String title; private String author; private int pubDate; // Construtor Book(String t, String a, int d) { title = t; author = a; pubDate = d; } // Método para exibir os detalhes do livro void show() { System.out.println(title); System.out.println(author); System.out.println(pubDate); System.out.println(); } } // Classe para demonstrar o uso de Book class BookDemo { public static void main(String args[]) { Book books[] = new Book[5]; // Cria uma matriz de objetos Book // Preenche a matriz com diferentes livros books[0] = new Book("Java: A Beginner's Guide", "Schildt", 2014); books[1] = new Book("Java: The Complete Reference", "Schildt", 2014); books[2] = new Book("The Art of Java", "Schildt and Holmes", 2003); books[3] = new Book("Red Storm Rising", "Clancy", 1986); books[4] = new Book("On the Road", "Kerouac", 1955); // Exibe os detalhes de cada livro for (int i = 0; i < books.length; i++) { books[i].show(); } } }
Compilation and Execution
javac bookpack/BookDemo.java
java bookpack.BookDemo
Important Explanations:
Expected Output:
Java: A Beginner's Guide Schildt 2014 Java: The Complete Reference Schildt 2014 The Art of Java Schildt and Holmes 2003 Red Storm Rising Clancy 1986 On the Road Kerouac 1955
The above is the detailed content of Brief Package Example. For more information, please follow other related articles on the PHP Chinese website!