0

我正在为图书馆制作程序;图书馆包含不同的项目,如书籍、视频、期刊等......

我有一个item用库中每个项目的基本方法调用的类,我的、book和所有其他项目的类都扩展了. 我想创建一个数组来包含库中的所有项目,以便人们可以搜索特定项目,它会在数组中找到它并显示该项目的信息。videojournalItem

我遇到的问题是,当我创建一个类型的数组时,Item我只能调用Item类的方法,而不能调用我bookbook类或类journal中创建的新方法journal

所以我不得不为每种不同类型的Items. 有没有办法只有一个数组可以为每个项目调用来自所有不同类的方法?

你可以在下面看到我的代码;我只附上了tester数组所在类的代码,如果您需要查看其他类的代码,请告诉我。我将不胜感激任何帮助。

import java.util.Arrays;
import java.util.Scanner;
public class Library{
  static String title;
  static String author;
  static int id;
  static int copies;
  static String date;
  static Book[] database = new Book[100];
  static Video[] database2 = new Video[100];
  static Journal[] database3 = new Journal[100];
  static CD[] database4 = new CD[100];
  static int count=0;

  public static void main(String[] args){
    int i;
    Scanner s = new Scanner(System.in);
    do{
      System.out.println("type the adjacent number to preform that process.\n1: Add an item\n2: Checkout an item\n0: Exit program");
      i=s.nextInt();
      switch(i){
        case 1:
          addItem();
          break;
        case 2: 

          break;
      }
    }while(i != 0);
    database[0].viewDetails();
    database[1].viewDetails();
    checkingOut();
    database[0].viewDetails();
    database[1].viewDetails();
  }
  public static void addItem(){
    int i;
    Scanner s = new Scanner(System.in);
    do{
      System.out.println("type the adjacent number to add the item.\n1: Book\n2: Video\n3: Journal\n4: CD\n Type 0 to stop");
      i=s.nextInt();
      switch(i){
        case 1:
          addBook();
          break;
        case 2: 
          addVideo();
          break;
        case 3:
          addJournal();
          break;
        case 4:
          addCD();
          break;
      }
    }while(i != 0);
  }
  public static void addBook(){
    String title;
    String author;
    int id;
    int copies;
    String date;
    int count=0;

    Scanner s = new Scanner(System.in);
    System.out.println("Enter the title of the book you want to add to the collection");
    title=s.nextLine();
    System.out.println("Enter the author of the book you want to add to the collection");
    author=s.nextLine();
    System.out.println("Enter the publishing date of the book you want to add to the collection");
    date=s.nextLine();
    System.out.println("Enter the ID number of the book you want to add to the collection");
    id=s.nextInt();
    System.out.println("Enter the the number of copies that will be added into the collection");
    copies=s.nextInt();

    Book Book1 = new Book(date, author, copies, id, title);
    database[count] = Book1;
    count++;
  }
  public static void addJournal(){
    String title;
    String author;
    int id;
    int copies;
    String date;
    int count=0;

    Scanner s = new Scanner(System.in);
    System.out.println("Enter the title of the journal you want to add to the collection");
    title=s.nextLine();
    System.out.println("Enter the author of the journal you want to add to the collection");
    author=s.nextLine();
    System.out.println("Enter the publishing date of the journal you want to add to the collection");
    date=s.nextLine();
    System.out.println("Enter the ID number of the journal you want to add to the collection");
    id=s.nextInt();
    System.out.println("Enter the the number of copies that will be added into the collection");
    copies=s.nextInt();

    Journal Journal1 = new Journal(date, author, copies, id, title);
    database3[count] = Journal1;
    count++;
  }
  public static void addCD(){
    String title;
    String art;
    String genre;
    int id;
    int copies;
    int date;
    int count=0;

    Scanner s = new Scanner(System.in);
    System.out.println("Enter the title of the cd you want to add to the collection");
    title=s.nextLine();
    System.out.println("Enter the artist of the cd you want to add to the collection");
    art=s.nextLine();
    System.out.println("Enter the release year of the cd you want to add to the collection");
    date=s.nextInt();
    System.out.println("Enter the genre of the cd you want to add to the collection");
    genre=s.nextLine();
    System.out.println("Enter the ID number of the cd you want to add to the collection");
    id=s.nextInt();
    System.out.println("Enter the the number of copies that will be added into the collection");
    copies=s.nextInt();

    CD CD1 = new CD(date, copies, id, title, art, genre);
    database4[count] = CD1;
    count++;
  }

  public static void addVideo(){
    String title;
    String director;
    int id;
    int copies;
    int date;
    String genre;
    int count=0;

    Scanner s = new Scanner(System.in);
    System.out.println("Enter the title of the video you want to add to the collection");
    title=s.nextLine();
    System.out.println("Enter the director of the cd you want to add to the collection");
    director=s.nextLine();
    System.out.println("Enter the release year of the cd you want to add to the collection");
    date=s.nextInt();
    System.out.println("Enter the genre of the video you want to add to the collection");
    genre=s.nextLine();
    System.out.println("Enter the ID number of the video you want to add to the collection");
    id=s.nextInt();
    System.out.println("Enter the the number of copies that will be added into the collection");
    copies=s.nextInt();

    Video Video1 = new Video(date, copies, id, title, director, genre);
    database2[count] = Video1;
    count++;
  }

  public static void checkingOut(){
    boolean found=false;
    int idSearch;
    int i=0;
    Scanner s=new Scanner(System.in);
    System.out.println("Enter the ID number of the book you want to check out");
    idSearch=s.nextInt();
    while(i<database.length && found!=true){
      if(database[i].getIdentificationNumber() == idSearch){
        found = true;
        break;
      }
      i++;
    }
    if(found==true){
      database[i].checkOut();
      System.out.println("There are "+database[i].getNumberCopies()+" copies left");
    }
    else{System.out.println("There is no book with that ID number!");}
  }
}
4

1 回答 1

2

您可以检查类型并将其强制转换为该类型以访问其他方法。

public static void main(String args[]){
    for(Item item : itemList){
    if(item instanceof Book){
        Book book = (Book) item;
        book.someBookMethod();
     }
    }
}

尽管使用instanceof认为是有害的

更好的设计是Item抽象并创建所有孩子共有的方法,例如add()

public abstract class Item{
     public void add();
     public void viewDetails();
}

class Book extends Item{
    public void add(){
    //fun stuff
    }
    public void viewDetails(){
    //moar serious business logic
   }
}
....
public static void main(String args[]){
for(Item item : itemList){
    item.add();
}
}
于 2013-11-08T16:32:08.613 回答