gpt4 book ai didi

java - 添加到集合 - 继承

转载 作者:搜寻专家 更新时间:2023-11-01 03:30:16 25 4
gpt4 key购买 nike

我正在尝试制作一个项目,将来自 main() 的 cd/dvd/movie 信息添加到 Collection 库然后打印添加的信息。喜欢:输出

-Book-
author: Robert A. Heinlein
# pages: 325
title: Starship Troopers
keywords: science fiction, war, weapons

-Music-
band: five finger death punch
# songs: 15
members: Zoltan Bathory, Ivan Moody,Jeremy Spencer,Matt Snell,Jason Hook
title: War is the answer
keywords: rock

我目前有 6 个类(class)

1.project1 - main()

2.Library - 我添加到数据库的地方

3.item - 继承(标题和编号)

4.cd

5.DVD

6.电影

我正在尝试使用继承,所以我想保留我拥有的文件。我的问题是我正在尝试添加到库类中的集合。我只是不确定该怎么做。

这是我认为你需要看的类(class)..

import java.io.PrintStream;
import java.util.Collection;



public class project
{
private static Library library = new Library();

public static void main(String[] args)
{
PrintStream out = System.out; // we will be printing to the standard output stream
Item item;



// add items to library
out.println(">>> adding items to library:\n");
item = library.addBook("The Curious Incident of the Dog in the Night-Time", "Mark Haddon", 240, "autism", "Asperger's Syndrome");
if (item != null)
library.printItem(out, item);
item = library.addBook("Starship Troopers", "Robert A. Heinlein", 325, "science fiction", "war", "weapons");
if (item != null)
library.printItem(out, item);
item = library.addBook("The Moon Is A Harsh Mistress", "Robert A. Heinlein", 389, "science fiction", "moon", "social structures");
if (item != null)
library.printItem(out, item);
item = library.addMusicCD("Europe In '72", "Grateful Dead", 12, "acid rock", "sixties", "jam bands");
if (item != null) {
library.addBandMembers(item, "Jerry Garcia", "Bill Kreutzman", "Keith Godcheaux");
library.printItem(out, item);
}
item = library.addMusicCD("Don't Let Go", "Jerry Garcia Band", 15, "acid rock", "jam bands");
if (item != null) {
library.addBandMembers(item, "Jerry Garcia", "Keith Godcheaux");
library.printItem(out, item);
}
item = library.addMusicCD("Sergeant Pepper's Lonely Hearts Club Band", "Beatles", 10, "acid rock", "sixties");
if (item != null) {
library.addBandMembers(item, "John Lennon", "George Harrison", "Ringo Starr");
library.printItem(out, item);
}
item = library.addMovieDVD("Lost In Translation", "Sofia Coppola", 14, "Japan", "loneliness");
if (item != null) {
library.addCast(item, "Bill Murray", "Scarlett Johansson");
library.printItem(out, item);
}
item = library.addMovieDVD("Groundhog Day", "Harold Ramis", 14, "newscaster", "groundhog", "time");
if (item != null) {
library.addCast(item, "Bill Murray", "Andie MacDowell");
library.printItem(out, item);
}

// print books, musicCDs, movies
out.println(">>> books:\n");
printItems(out, library.books());
out.println(">>> music CDs:\n");
printItems(out, library.musicCDs());
out.println(">>> movies:\n");
printItems(out, library.movies());

// print items for keyword
printItemsForKeyword(out, "science fiction");
printItemsForKeyword(out, "jam bands");
printItemsForKeyword(out, "xxx");

// items by artist
out.println(">>> books by Robert A. Heinlein:\n");
printItems(out, library.booksByAuthor("Robert A. Heinlein"));
out.println(">>> music by the Grateful Dead:\n");
printItems(out, library.musicByBand("Grateful Dead"));
out.println(">>> music by the Rolling Stones:\n");
printItems(out, library.musicByBand("Rolling Stones"));
out.println(">>> movies by Sofia Coppola:\n");
printItems(out, library.moviesByDirector("Sofia Coppola"));
out.println(">>> music by Jerry Garcia:\n");
printItems(out, library.musicByMusician("Jerry Garcia"));
out.println(">>> movies with Bill Murray:\n");
printItems(out, library.moviesByActor("Bill Murray"));
}

private static void printItemsForKeyword (PrintStream out, String keyword)
{
Collection<Item> items;

out.printf(">>> items for keyword: %s\n\n", keyword);
items = library.itemsForKeyword(keyword);
printItems(out, items);
}

private static void printItems (PrintStream out, Collection<Item> items)
{
if (items != null && items.size() > 0)
for (Item item : items)
library.printItem(out, item);
else
out.println("none\n");
}
}

这是我在添加到集合时遇到问题的库类..

我如何将书籍或 CD 添加到 Collection 中?

import java.io.PrintStream;
import java.util.Collection;
import java.util.*;

public class Library
{


// returns all of the items which have the specified keyword
public Collection<Item> itemsForKeyword(String keyword)
{
return null;
}

// print an item from this library to the output stream provided
public void printItem(PrintStream out, Item item)
{
}



// adds a book to the library
public Item addBook(String title, String author, int nPages, String... keywords)
{


return null;
}

// returns all of the books by the specified author
public Collection<Item> booksByAuthor(String author)
{
return null;
}

// returns all of the books in the library
public Collection<Item> books()
{
return null;
}

// music-related methods

// adds a music CD to the library
public Item addMusicCD(String title, String band, int nSongs, String... keywords)
{
Collection MusicCollection = new HashSet();

MusicCollection.add(title);
return null;
}

// adds the specified band members to a music CD
public void addBandMembers(Item musicCD, String... members)
{
}

// returns all of the music CDs by the specified band
public Collection<Item> musicByBand(String band)
{
return null;
}

// returns all of the music CDs by the specified musician
public Collection<Item> musicByMusician(String musician)
{
return null;
}

// returns all of the music CDs in the library
public Collection<Item> musicCDs()
{
return null;
}

// movie-related methods

// adds a movie to the library
public Item addMovieDVD(String title, String director, int nScenes, String... keywords)
{
return null;
}

// adds the specified actors to a movie
public void addCast(Item movie, String... members)
{
}

// returns all of the movies by the specified director
public Collection<Item> moviesByDirector(String director)
{
return null;
}

// returns all of the movies by the specified actor
public Collection<Item> moviesByActor(String actor)
{
return null;
}

// returns all of the movies in the library
public Collection<Item> movies()
{
return null;
}
}

这是项目类

import java.io.PrintStream;
import java.util.Collection;
import java.util.*;

public class Item
{
private String title;
private int number;

public Item(String theTitle, int theNumber)
{
number = theNumber;
title = theTitle;

}

public String getTitle()
{
return title;
}

public int getNumber()
{
return number;
}

}

这是 cd 类 - dvd 类几乎相同

import java.util.*;


public class CD extends Item
{

private String artist;
private String members;


public CD(String theTitle, String theArtist, String theMembers, int number)
{
super(theTitle,number);
artist = theArtist;
members = theMembers;

}


public String getArtist()
{

return artist;

}

public String getMembers()
{
return members;
}

public void print()
{
System.out.println("-Music-");
System.out.println("band: " + artist);
}

}

我不确定是否可以将 cd/dvd/movie 类合并到项目类中?

我的主要问题是:

我应该如何将每张 cd/dvd 添加到 Collection 夹?????

在库类中

我是要定义一个集合以添加到每个附加功能(addMusicCD、addBandMembers、addMovieDVD 等)中,还是应该将集合放在类(class)的开头?以及如何添加到该集合???

public Item addMusicCD(String title, String band, int nSongs, String... keywords)
{
Collection MusicCollection = new HashSet(); // how should i add each cd/dvd to collections?????

MusicCollection.add(title);
return null;
}

我也想返回但不能!我需要退回什么元素?我知道这是很多信息。对不起。希望有人能帮助我,而不是只是取笑我。我正在尝试从 C++ 学习 Java

谢谢你能给我的任何帮助..

最佳答案

我认为您需要多学习一点面向对象原则。

图书馆应该能够添加项目。库方法 addBook(many params) 和 addDVD() 等应替换为更通用的 addItem(Item item, String... keywords)。

项目可以是 CD、DVD 或电影。添加乐队成员的是 CD 类,而不是 Library 类。将项目添加到库中变成类似

CD cd = new CD("Europe In '72", "Grateful Dead", 12);
cd.addBandMembers("Jerry Garcia", "Bill Kreutzman", "Keith Godcheaux");
library.addItem(cd, "acid rock", "sixties", "jam bands"));

希望这有助于您走上正轨。

关于java - 添加到集合 - 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2270295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com