Home > Java > javaTutorial > body text

Brief Package Example

DDD
Release: 2024-09-29 06:08:02
Original
807 people have browsed it

Exemplo Breve de Pacote

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

Copy after login
  1. Defining the Book Class within the bookpack Package (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();
        }
    }
}

Copy after login

Compilation and Execution

  1. Compiling the Code From the above bookpack directory, compile the file with:
javac bookpack/BookDemo.java

Copy after login
  1. Running the Program Run the program from the current working directory with:
java bookpack.BookDemo

Copy after login

Important Explanations:

  • Bookpack package: Both Book and BookDemo are part of the bookpack package. This means that to run the program, the package name must be specified.
  • Run Command: When running BookDemo, the full package name is required (bookpack.BookDemo). Running java BookDemo without specifying the package will not work.

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

Copy after login

The above is the detailed content of Brief Package Example. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template