JAVA 类与对象 求符合下列要求的代码 10
JAVA类与对象求符合下列要求的代码1)定义 Book 类,包含书名,作者,页数和价格成员;2)能够正确定义构造方法初始化数据成员;3)定义成员方法s...
JAVA 类与对象
求符合下列要求的代码1)定义 Book 类,包含书名,作者,页数和价格成员;
2)能够正确定义构造方法初始化数据成员;
3)定义成员方法 showInfo,依次显示书的成员变量的内容;
4)定义成员方法 renew,更新书名、作者、页数和价格;
5)在程序中定义类的对象,并正确使用其成员方法。 展开
求符合下列要求的代码1)定义 Book 类,包含书名,作者,页数和价格成员;
2)能够正确定义构造方法初始化数据成员;
3)定义成员方法 showInfo,依次显示书的成员变量的内容;
4)定义成员方法 renew,更新书名、作者、页数和价格;
5)在程序中定义类的对象,并正确使用其成员方法。 展开
2个回答
2017-12-19 · 百度知道合伙人官方认证企业
尚学堂科技有限公司
北京尚学堂科技有限公司是一家从事JAVA、大数据、web前端、网络在线教育、互联网架构师、Android、ios技术开发、咨询为一体的软件公司。公司由海外留学人员和国内IT人士创建。
向TA提问
关注
展开全部
Book相关代码:
public class Book {
private String name;
private String author;
private long page_num;
private double price;
// 含参构造方法用于初始化数据
public Book(String name, String author, long page_num, double price) {
this.name = name;
this.author = author;
this.page_num = page_num;
this.price = price;
}
// 显示书的成员变量的内容
public void showInfo() {
System.out.println(
"Book [name=" + name + ", author=" + author + ", page_num=" + page_num + ", price=" + price + "]");
}
// 更新书名、作者、页数和价格
public void renew(Book book) {
Scanner scanner = new Scanner(System.in);
System.out.println("把书名修改成:");
String new_book = scanner.nextLine();
book.setName(new_book);
System.out.println("把作者修改成:");
String new_author = scanner.nextLine();
book.setAuthor(new_author);
System.out.println("页码是::");
long long1 = scanner.nextLong();
book.setPage_num(long1);
System.out.println("价格是::");
double double1 = scanner.nextDouble();
book.setPrice(double1);
System.out.println(
"NewBook [name=" + name + ", author=" + author + ", page_num=" + page_num + ", price=" + price + "]");
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
public long getPage_num() {
return page_num;
}
public double getPrice() {
return price;
}
public void setName(String name) {
this.name = name;
}
public void setAuthor(String author) {
this.author = author;
}
public void setPage_num(long page_num) {
this.page_num = page_num;
}
public void setPrice(double price) {
this.price = price;
}
}
测试类代码以及测试效果:
展开全部
Book.java
package demo19;
public class Book {
private String name;
private String author;
private int page;
private float price;
public Book(String name, String author, int page, float price) {
this.name = name;
this.author = author;
this.page = page;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public void showInfo() {
System.out.println("书名:" + this.name);
System.out.println("作者:" + this.author);
System.out.println("页数:" + this.page);
System.out.println("价格:" + this.price);
}
public void renew(String name, String author, int page, float price) {
this.name = name;
this.author = author;
this.page = page;
this.price = price;
}
}
App.java
package demo19;
public class App {
public static void main(String[] args) {
Book book = new Book("Java编程思想", "Bruce Eckel", 500, 86.4f);
book.showInfo();
book.renew("Java编程思想", "Bruce Eckel", 700, 86.4f);
book.showInfo();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询