编写一个简单的Java程序:在一个图书管理程序中,类Book.Newspsper和 Video都是类Media 的子类。编写一个类,该类前够实现对一组书、报纸等的存储,并提供一定的检索功能。
时间: 2024-06-09 15:07:41 浏览: 109
下面是一个简单的Java程序,实现了一个图书管理程序,包含了类Book、Newspaper和Video,它们都是类Media的子类。另外还实现了一个类MediaLibrary,用于存储和检索一组图书、报纸和视频。
```java
public class Media {
private String title;
private String author;
private int year;
public Media(String title, String author, int year) {
this.title = title;
this.author = author;
this.year = year;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYear() {
return year;
}
}
public class Book extends Media {
private String publisher;
public Book(String title, String author, int year, String publisher) {
super(title, author, year);
this.publisher = publisher;
}
public String getPublisher() {
return publisher;
}
}
public class Newspaper extends Media {
private String editor;
public Newspaper(String title, String author, int year, String editor) {
super(title, author, year);
this.editor = editor;
}
public String getEditor() {
return editor;
}
}
public class Video extends Media {
private String director;
public Video(String title, String author, int year, String director) {
super(title, author, year);
this.director = director;
}
public String getDirector() {
return director;
}
}
import java.util.ArrayList;
import java.util.List;
public class MediaLibrary {
private List<Media> mediaList;
public MediaLibrary() {
mediaList = new ArrayList<>();
}
public void addMedia(Media media) {
mediaList.add(media);
}
public List<Media> searchByTitle(String title) {
List<Media> result = new ArrayList<>();
for (Media media : mediaList) {
if (media.getTitle().equals(title)) {
result.add(media);
}
}
return result;
}
public List<Media> searchByAuthor(String author) {
List<Media> result = new ArrayList<>();
for (Media media : mediaList) {
if (media.getAuthor().equals(author)) {
result.add(media);
}
}
return result;
}
public List<Media> searchByYear(int year) {
List<Media> result = new ArrayList<>();
for (Media media : mediaList) {
if (media.getYear() == year) {
result.add(media);
}
}
return result;
}
}
```
使用示例:
```java
MediaLibrary library = new MediaLibrary();
// 添加一些书、报纸和视频
library.addMedia(new Book("Java编程思想", "Bruce Eckel", 2002, "机械工业出版社"));
library.addMedia(new Book("Effective Java", "Joshua Bloch", 2008, "机械工业出版社"));
library.addMedia(new Newspaper("人民日报", "人民日报社", 2020, "张宏伟"));
library.addMedia(new Newspaper("新京报", "新京报社", 2020, "刘畅"));
library.addMedia(new Video("肖申克的救赎", "弗兰克·德拉邦特", 1994, "弗兰克·德拉邦特"));
// 按照标题搜索
List<Media> result1 = library.searchByTitle("Java编程思想");
for (Media media : result1) {
System.out.println(media.getTitle() + " (" + media.getYear() + ")");
}
// 按照作者搜索
List<Media> result2 = library.searchByAuthor("Joshua Bloch");
for (Media media : result2) {
System.out.println(media.getTitle() + " (" + media.getYear() + ")");
}
// 按照年份搜索
List<Media> result3 = library.searchByYear(2020);
for (Media media : result3) {
System.out.println(media.getTitle() + " (" + media.getAuthor() + ")");
}
```
阅读全文