//项目类别
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/