
书包:
该示例创建一个名为 bookpack 的包,其中包含一个用于管理书籍数据库的简单类。
图书课程:
它有私有属性 title、author 和 pubdate(标题、作者和出版日期)。
构造方法初始化属性。
show() 方法显示书籍详细信息。
图书演示课程:
创建一个包含 5 个 book 对象的数组。
用书籍信息填充数组并使用 show() 方法显示详细信息。
代码示例
目录结构:
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();
}
}
}
编译执行
javac bookpack/bookdemo.java
java bookpack.bookdemo
重要说明:
预期输出:
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
以上就是简要封装示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号