java - 使用接口(interface)在 java 中运行时绑定(bind)

标签 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 !!!");    
            }
        }
    }
}

As in this code i have created two classes i.e.. Item and Libraryy . At run time dynamic binding is done successfully but after reading any choice it shows results at the same time and i want to show all results after entering all the choices first .

For storing references i used arrays which stores the refernce of my choice types.

最佳答案

**//类项目很好**

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();                          


      }
}

关于java - 使用接口(interface)在 java 中运行时绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55433406/

相关文章:

Java - 从终端运行 jar 可以,但双击会破坏功能

javaFX 文本字段和监听器

java - Spark 1.6 : How do convert an RDD generated from a Scala jar to a pyspark RDD?

java - SWT进度条的标准rgb颜色是什么

ruby - 异常类如何获得引发它的对象?

wpf - Combobox.SelectedItem 绑定(bind)到嵌套属性

WPF 验证问题 : how to work with validation which involves 2 fields

c# - WPF MVVM 创建多个数据网格并将项目绑定(bind)到它们

java - 将自定义字段添加到 Spark ML LabeldPoint

java - 如何在 Java 中实现 lower_bound 二进制搜索算法?