【问题标题】:having trouble setting up an array for my program为我的程序设置数组时遇到问题
【发布时间】:2013-11-09 00:42:24
【问题描述】:

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

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

我遇到的问题是,当我创建Item 类型的数组时,我只能调用Item 类的方法,而我不能在@ 中调用我为book 创建的新方法987654329@ 类或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!");}
  }
}

【问题讨论】:

  • 如果将它们放在一起,数组如何知道它正在使用的项目类型?

标签: java


【解决方案1】:

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

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

虽然使用instanceofconsidered harmful

更好的设计是将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();
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2011-04-10
    • 2013-05-06
    • 1970-01-01
    相关资源
    最近更新 更多