求一道java作业题
下面是我写的代码,分两个类写的。
①Book.java
public class Book {
private String bookname;
private String author;
private int sales;
public Book() {
this.bookname = "";
this.author = "";
this.sales = 0;
}
public Book(String bookname, String author, int sales) {
this.bookname = bookname;
this.author = author;
this.sales = sales;
}
public void setBook(String bookname, String author, int sales) {
this.bookname = bookname;
this.author = author;
this.sales = sales;
}
public void printBook() {
System.out.print(this.bookname);
System.out.print("\t");
System.out.print("\t");
System.out.print(this.author);
System.out.print("\t");
System.out.print("\t");
System.out.println(this.sales);
}
}
②TestBook.java
public class TestBook {
public static void main(String[] args) {
Book b1 = new Book();
Book b2 = new Book();
Book b3 = new Book();
Book b4 = new Book();
Book b5 = new Book("万古神帝", "飞天鱼", 342);
Book b6 = new Book("阴阳师", "小墨鱼", 470);
Book b7 = new Book("斗罗大陆", "唐家三少", 207);
Book b8 = new Book("完美世界", "辰东", 214);
Book b9 = new Book("亮剑", "都梁", 78);
b1.setBook("天龙八部", "金庸", 150);
b2.setBook("盗墓笔记", "南派三叔", 420);
b3.setBook("元尊", "天蚕土豆", 368);
b4.setBook("都灵", "红色警戒", 42);
System.out.print("书名");
System.out.print("\t");
System.out.print("\t");
System.out.print("作者");
System.out.print("\t");
System.out.print("\t");
System.out.println("月销售量");
b1.printBook();
b2.printBook();
b3.printBook();
b4.printBook();
b5.printBook();
b6.printBook();
b7.printBook();
b8.printBook();
b9.printBook();
}
}
下面是我做的测试结果的截图,麻烦帮忙看一下,是否能够符合要求。
public class Book
{
private String name;
private String author;
private int sales;
public Book()
{
}
public Book(String name,String author,int sales)
{
this.name = name;
this.author = author;
this.sales = sales;
}
public void setBook(String name,String author,int sales)
{
this.name = name;
this.author = author;
this.sales = sales;
}
public void printBook()
{
System.out.println("书名:" + name +
", 作者:" + author +
", 月销量:" + sales);
}
}
===================================================
public class TestBook
{
public static void main(String[] args) {
Book book = new Book();
book.setBook("《西游记》","吴承恩",1115);
book.printBook();
}
}
public class Book {
private String bookName;
private String author;
private int salesCount;
public Book() {}
public Book(String bookName, String author, int salesCount) {
this.bookName = bookName;
this.author = author;
this.salesCount = salesCount;
}
public void setBook(String bname, String auth, int count) {
this.bookName=bname;
this.author=auth;
this.salesCount=count;
}
public String printBook() {
return "书名:"+this.bookName+"\t 作者:"+this.author+"\t 销量:"+this.salesCount;
}
}
this.name=name;……
}
printBook()应该只是为了显示用System.out.println()就好了,不是为了返回值。
大佬能不能给个完整的代码参考参考
Book类:
public class Book {
private String name=null;
private String author=null;
private int monthlySales=0;
public Book(){}
public Book(String name,String author,int monthlySales){
this.name=name;
this.author=author;
this.monthlySales=monthlySales;
}
public void set(String name,String author,int monthlySales){
this.name=name;
this.author=author;
this.monthlySales=monthlySales;
}
public void printBook(){
System.out.println("《"+name+"》作者:"+author+",月销量:"+monthlySales);
}
}
TsetBook类:
public class TestBook {
public static void main(String[] args) {
Book book1=new Book();
book1.set("红楼梦","曹雪芹",37000);
Book book2=new Book("三国演义","罗贯中",29000);
book1.printBook();
book2.printBook();
}
}