使用接口在Java中运行时绑定

时间:2019-03-30 16:21:14

标签: java binding

//物品类别

import java.io.*;

    interface Item {
        void read();
          void show();
    }

    class Book implements Item {

         String name,author,publication;
         public void read() {
         Console con  = System.console();

        System.out.println("Enter Name of the Book:");
        name = con.readLine();

        System.out.println("Enter Author Name:");
        author = con.readLine();

        System.out.println("Enter Publication of the book:");
        publication = con.readLine();  
    }


         public void show() {
         System.out.println("List Of Issued Items");
          System.out.println("Name  :"+name);
              System.out.println("Author  :"+author);
             System.out.println("Publication :"+publication);
        } 
    }

       class Dvd implements Item {

       String dname,director,category;

        public void read() {
        Console con  = System.console();

         System.out.println("Enter Name of the dvd ");
         dname = con.readLine();

         System.out.println("Enter Director Name");
         director = con.readLine();

         System.out.println("Enter Category of the dvd: ");
         category = con.readLine();
        }

         public void show() {
         System.out.println("List Of Issued Items");
           System.out.println("Name  :"+dname);
           System.out.println("Director  :"+director);
         System.out.println("Category :"+category);
        } 
      }

图书馆课

import java.io.*;
class Library {
    public static void main(String args[]) {

        Console con  = System.console();
        Item arr[] = new Item[2];
        Item a;

        for(int i=0;i<arr.length;i++) {
            System.out.println("Enter Your Choice : < b / d >");
            String ch = con.readLine();


        switch(ch) {
            case "b": 
                a = new Book();
                a.read();
                a.show();
                break;

            case "d": 
                a = new Dvd();
                a.read();
                a.show();
                break;

            default:
            System.out.println(" You Enetred The Wrong Choice !!!");    
            }
        }
    }
}
  

如该代码中所示,我创建了两个类,即Item和Libraryy。   在运行时,动态绑定成功完成,但是在读取任何选择后,它会同时显示结果,我想先输入所有选择后再显示所有结果。

     

为了存储引用,我使用了存储我选择类型的引用的数组。

1 个答案:

答案 0 :(得分:0)

** // Class Item很好**

import java.io.*;

interface Item {
    void read();
      void show();
}

class Book implements Item {

     String name,author,publication;
     public void read() {
     Console con  = System.console();

    System.out.println("Enter Name of the Book:");
    name = con.readLine();

    System.out.println("Enter Author Name:");
    author = con.readLine();

    System.out.println("Enter Publication of the book:");
    publication = con.readLine();  
}


     public void show() {
     System.out.println("List Of Issued Items");
      System.out.println("Name  :"+name);
          System.out.println("Author  :"+author);
         System.out.println("Publication :"+publication);
    } 
}

   class Dvd implements Item {

   String dname,director,category;

    public void read() {
    Console con  = System.console();

     System.out.println("Enter Name of the dvd ");
     dname = con.readLine();

     System.out.println("Enter Director Name");
     director = con.readLine();

     System.out.println("Enter Category of the dvd: ");
     category = con.readLine();
    }

     public void show() {
     System.out.println("List Of Issued Items");
       System.out.println("Name  :"+dname);
       System.out.println("Director  :"+director);
     System.out.println("Category :"+category);
    } 
  }

//类库

import java.io.*;
class Library {
    public static void main(String args[]) {

    Console con  = System.console();
        Item arr[] = new Item[2];
        Item a;

        for(int i=0;i<arr.length;i++) 
    {
        System.out.println("Enter Your Choice : < b / d >");
            String ch = con.readLine();


                   switch(ch) 
        {

            case "b": 
                arr[i] = new Book();
                arr[i].read();
            break;

            case "d": 
                arr[i] = new Dvd();
            arr[i].read();
            break;

            default:
            System.out.println(" You Enetred The Wrong Choice !!!");    
            }
            }

             for(int i=0;i<arr.length;i++)
                arr[i].show();                          


      }
}