如何将子类的字段值与超类的字段值一起存储到 ArrayList 中? - Java
How to store the field values of subclass along with superclass field values into an ArrayList? - Java
我需要使用 class,它有一个 LibraryItem 类型的 ArrayList。我需要存储 superclass LibraryItem 及其 subclasses Book 和 Periodical 在此 ArrayList 中(程序读取数据文件并将每个单词相应地存储在适当的字段中)。我该怎么做?
Class - Library(class ArrayList of LibraryItem)
public class Library
{
private ArrayList<LibraryItem> itemList; // it declares an ArrayList of LibraryItem type
public Library()
{
itemList = new ArrayList<>(); // it initalises the ArrayList of LibraryItem type
}
public void storeItem(LibraryItem libraryItem)
{
itemList.add(libraryItem);
}
// other codes omitted
if (typeOfData.equals("Book"))
{
System.out.println(lineOfText);
Scanner scanner2 = new Scanner(lineOfText);
LibraryItem libraryItem = new Book();
scanner2.useDelimiter("[,\n]");
libraryItem.readData(scanner2);
storeItem(libraryItem);
scanner2.close(); // ends scanner2
}
else if (typeOfData.equals("Periodical"))
{
System.out.println(lineOfText);
Scanner scanner2 = new Scanner(lineOfText);
LibraryItem libraryItem = new Periodical();
scanner2.useDelimiter("[,\n]");
libraryItem.readData(scanner2);
storeItem(libraryItem);
scanner2.close(); // ends scanner2
}
// other codes omitted
}
Class - 图书馆项目 (超级class)
public class LibraryItem
{
// fields omitted
public LibraryItem()
{
}
/**
* Mutator method used to read and store each data in the appropriate field
* @param <code>scanner2</code> Scanner is used to pass a Scanner object
* containg matching values with the field datatypes
*/
public void readData(Scanner scanner2)
{
scanner2.useDelimiter("\s?,\s?");
noOfPages = scanner2.nextInt();
publisher = scanner2.next();
title = scanner2.next();
itemCode = scanner2.next();
cost = scanner2.nextInt();
timesBorrowed = scanner2.nextInt();
onLoan = scanner2.nextBoolean();
}
// other codes omitted
}
Class - 书 (subclass)
public class Book extends LibraryItem
{
private String author;
private String isbn;
public Book(String author, String isbn, int noOfPages, String publisher)
{
super();
this.author = author;
this.isbn = isbn;
}
public Book()
{
}
@Override
public void readData(Scanner scanner2)
{
scanner2.useDelimiter("[,\n]");
author = scanner2.next();
isbn = scanner2.next().trim();
super.readData(scanner2);
}
// other codes omitted
}
Class - 定期(子class)
public class Periodical extends LibraryItem
{
private String publicationDate;
public Periodical(String publicationDate)
{
super();
this.publicationDate = publicationDate;
}
public Periodical()
{
}
@Override
public void readData(Scanner scanner2)
{
scanner2.useDelimiter("[,\n]");
publicationDate = scanner2.next();
super.readData(scanner2);
}
解决方案是这段代码在调用子类的字段时没有任何问题。我需要在子类 Book 和 Periodical:
中创建一个 overridden 方法
Class - 图书馆
LibraryItem is a polymorphic variable
public void printAllItems()
{
for (LibraryItem call : itemList)
{
call.printDetails(); // it will call the overridden method of each subclass
}
}
摘要Class - 图书馆项目
public void printDetails() // main method called by overridden methods
{
String notOnLoan = "";
if (onLoan == false)
notOnLoan = "not on";
else
notOnLoan = "on";
// these fields are used from both Book class and Periodical class
System.out.println(noOfPages + publisher);
System.out.println(title + itemCode + timesBorrowed);
System.out.println(notOnLoan + cost);
System.out.println();
}
子类 - Book(继承自 LibraryItem)
@Override
public void printDetails()
{
System.out.println(author + isbn);
super.printDetails(); // it will run the method of subclass
}
子类 - Periodical(继承自 LibraryItem)
@Override
public void printDetails()
{
System.out.println(publicationDate);
super.printDetails(); // it will run the method of subclass
}
我需要使用 class,它有一个 LibraryItem 类型的 ArrayList。我需要存储 superclass LibraryItem 及其 subclasses Book 和 Periodical 在此 ArrayList 中(程序读取数据文件并将每个单词相应地存储在适当的字段中)。我该怎么做?
Class - Library(class ArrayList of LibraryItem)
public class Library
{
private ArrayList<LibraryItem> itemList; // it declares an ArrayList of LibraryItem type
public Library()
{
itemList = new ArrayList<>(); // it initalises the ArrayList of LibraryItem type
}
public void storeItem(LibraryItem libraryItem)
{
itemList.add(libraryItem);
}
// other codes omitted
if (typeOfData.equals("Book"))
{
System.out.println(lineOfText);
Scanner scanner2 = new Scanner(lineOfText);
LibraryItem libraryItem = new Book();
scanner2.useDelimiter("[,\n]");
libraryItem.readData(scanner2);
storeItem(libraryItem);
scanner2.close(); // ends scanner2
}
else if (typeOfData.equals("Periodical"))
{
System.out.println(lineOfText);
Scanner scanner2 = new Scanner(lineOfText);
LibraryItem libraryItem = new Periodical();
scanner2.useDelimiter("[,\n]");
libraryItem.readData(scanner2);
storeItem(libraryItem);
scanner2.close(); // ends scanner2
}
// other codes omitted
}
Class - 图书馆项目 (超级class)
public class LibraryItem
{
// fields omitted
public LibraryItem()
{
}
/**
* Mutator method used to read and store each data in the appropriate field
* @param <code>scanner2</code> Scanner is used to pass a Scanner object
* containg matching values with the field datatypes
*/
public void readData(Scanner scanner2)
{
scanner2.useDelimiter("\s?,\s?");
noOfPages = scanner2.nextInt();
publisher = scanner2.next();
title = scanner2.next();
itemCode = scanner2.next();
cost = scanner2.nextInt();
timesBorrowed = scanner2.nextInt();
onLoan = scanner2.nextBoolean();
}
// other codes omitted
}
Class - 书 (subclass)
public class Book extends LibraryItem
{
private String author;
private String isbn;
public Book(String author, String isbn, int noOfPages, String publisher)
{
super();
this.author = author;
this.isbn = isbn;
}
public Book()
{
}
@Override
public void readData(Scanner scanner2)
{
scanner2.useDelimiter("[,\n]");
author = scanner2.next();
isbn = scanner2.next().trim();
super.readData(scanner2);
}
// other codes omitted
}
Class - 定期(子class)
public class Periodical extends LibraryItem
{
private String publicationDate;
public Periodical(String publicationDate)
{
super();
this.publicationDate = publicationDate;
}
public Periodical()
{
}
@Override
public void readData(Scanner scanner2)
{
scanner2.useDelimiter("[,\n]");
publicationDate = scanner2.next();
super.readData(scanner2);
}
解决方案是这段代码在调用子类的字段时没有任何问题。我需要在子类 Book 和 Periodical:
中创建一个 overridden 方法Class - 图书馆
LibraryItem is a polymorphic variable
public void printAllItems()
{
for (LibraryItem call : itemList)
{
call.printDetails(); // it will call the overridden method of each subclass
}
}
摘要Class - 图书馆项目
public void printDetails() // main method called by overridden methods
{
String notOnLoan = "";
if (onLoan == false)
notOnLoan = "not on";
else
notOnLoan = "on";
// these fields are used from both Book class and Periodical class
System.out.println(noOfPages + publisher);
System.out.println(title + itemCode + timesBorrowed);
System.out.println(notOnLoan + cost);
System.out.println();
}
子类 - Book(继承自 LibraryItem)
@Override
public void printDetails()
{
System.out.println(author + isbn);
super.printDetails(); // it will run the method of subclass
}
子类 - Periodical(继承自 LibraryItem)
@Override
public void printDetails()
{
System.out.println(publicationDate);
super.printDetails(); // it will run the method of subclass
}