我是Java&我一直在完成设定任务,到目前为止,我正在执行一项任务,要求我:
“3。设计并实现一个名为Library的类,它可以包含库中所有手册的详细信息。您的类应该(至少)有方法来添加手册并打印所有手册的详细信息。”
我已经有一个“手动”课程,如下所示(基于前2个任务集):
public class Manual {
String serialNumber, title, author;
public static void main(String [] args){
Manual man= new Manual();
man.print();
}
public Manual(){
set("??????", "Untitled", "Unknown");
}
public Manual(String serialNumber, String title, String author)
{
set(serialNumber, title, author);
}
public String getserialNumber()
{
return serialNumber;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public void set(String serialNumber, String title, String author)
{
this. serialNumber = serialNumber;
this. title = title;
this.author = author;
}
public void print()
{
System.out.println("Serial Number : " +serialNumber);
System.out.println("Title : " +title);
System.out.println("Author : " +author);
}
public void print(String heading)
{
System.out.println(heading);
print();
}
public void ask()
{
serialNumber = Console.askString("Please enter the serial number: ");
title = Console.askString("Please enter the title: ");
author = Console.askString("Please enter the author: ");
set(serialNumber, title, author);
}
public String toString()
{
return "SerialNumber: " + serialNumber +"\n"
+"Title: " + title + "\n"
+"Author: " + author +"\n";
}
public void print()
{
System.out.println(this.toString());
}
}
接下来是我试图正确完成的库类:
public class Library {
public void print(String header){
Manual.print(header); }
public void ask(String prompt) {
Manual.ask(prompt);
}
}
我是否设法根据我的图书馆课程中的内容充分完成了这个问题?如果有人能够教育我有任何错误,或者我缺少代码段以便我完成任务,我将非常感激。
谢谢
答案 0 :(得分:0)
以我的逻辑方式,认为它在这方面做得更好
///图书馆类
import java.util.*;
public class Library
{
public static void main(String[] args)
{
String serialNumber, title, author;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the serial number: ");
serialNumber = input.next();
System.out.print("Please enter the title: ");
title= input.next();
System.out.print("Please enter the author: ");
author = input.next();
Manual man= new Manual(serialNumber,title,author);
man.print();
}
}
///手动课
public class Manual
{
String serialNumber, title, author;
public Manual(String serialNumber, String title, String author)
{
setSerialNumber(serialNumber);
setTitle(title);
setAuthor(author);
}
//// serial number
public void setSerialNumber(String number)
{
serialNumber = number;
}
public String getserialNumber()
{
return serialNumber;
}
public void setTitle(String mytitle)
{
title = mytitle;
}
public String getTitle()
{
return title;
}
public void setAuthor(String myAuthor)
{
author = myAuthor;
}
public String getAuthor()
{
return author;
}
public void print()
{
System.out.println("Serial Number : " +serialNumber);
System.out.println("Title : " +title);
System.out.println("Author : " +author);
}
public void print(String heading)
{
System.out.println(heading);
printing();
}
public String toString()
{
return "SerialNumber: " + serialNumber +"\n"
+"Title: " + title + "\n"
+"Author: " + author +"\n";
}
public void printing()
{
System.out.println(this.toString());
}
}
作为建议,总是做简单的事情,简单但逻辑的代码。我也是像你这样的初学者。这就是我一直被告知的事情
希望它有用