1-7编程问题提问

来源:1-7 编程练习

Michael_2020

2020-01-19 18:27:21

老师好,关于这个练习我有几向没有弄明白,请赐教:

1、要求书名和作者为只读,那是否只能在构造方法中给书名和作者赋值?如果是这样,那如何对第二个书名和作者进行赋值?

2、方法类里已经有关于信息介绍的方法。我在主方法里直接调用就可以输出,如何能用到get/set方法?

写回答

4回答

好帮手慕小班

2020-01-19

同学你好,1、书名与作者为只读,同学可以在构造方法中,传入参数进行赋值,例如:

http://img.mukewang.com/climg/5e243a3d091fe41f05480269.jpg

    2、get/set方法的作用。

        如果当你对设置的参数有一定的要求时,例如年龄不可以为负数。同学可以通过在set方法中写个判断语句设置一下,并且在带参构造中调用set方法,这样,同学设置年龄时就被加了一层保障。

如果构造方法中按照原来的直接给属性赋值,则有可能不按照规则来设置。同理get方法也可以设置。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~

0
hichael_2020
h 您好老师。我在回答中重新贴了代码,麻烦您给看一下,是否符合题目的要求。 1、我尝试用带参构造,使用键盘输入价格和出版社(这里没有做异常处理) 2、我尝试用带参构造直接赋值, 3、我尝试用无参构造赋值,但无法去书名和作者进行赋值 以上都只用了set方法,没有用到get方法,请老师帮我答疑,以下是否合理。是否符合题目要求。感谢!
h020-01-20
共1条回复

Michael_2020

提问者

2020-01-20

package com.yito.book;


public class Book {


// 私有属性:书名、作者、出版社、价格

private String bookName;

private String author;

private String publisher;

private double price;


// 通过构造方法实现属性赋值

/*

* 通过公有的get/set方法实现属性的访问, 其中: 1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10 2、限定作者、书名均为只读属性

*/

public Book() {


}


public Book(String bookName, String author, String publisher, double price) {

this.bookName = bookName;

this.author = author;

this.setPublisher(publisher);

this.setPrice(price);

}


public String getBookName() {

return bookName;

}


public String getAuthor() {

return author;

}


public String getPublisher() {

return publisher;

}


public void setPublisher(String publisher) {

this.publisher = publisher;

}


public double getPrice() {

return price;

}


public void setPrice(double price) {

if (price < 10) {

System.out.println("图书价格最低10元。");

this.price = 10;

} else

this.price = price;

}


// 信息介绍方法,描述图书所有信息

public void note() {

System.out.println("书名:" + this.bookName);

System.out.println("作者:" + this.author);

System.out.println("出版社:" + this.publisher);

System.out.println("价格:" + this.price);

}


}

//=================================

package com.yito.book;


import java.util.Scanner;


public class BookTest {


public static void main(String[] args) {

// 带参构造

Scanner sc = new Scanner(System.in);

String bookName = "红楼梦";

String author = "曹雪芹";

System.out.println("书名:" + bookName);

System.out.println("作者:" + author);

System.out.println("请输入出版社名称");

String publisher = sc.next();

System.out.println("请输入价格");

double price = sc.nextDouble();

Book book = new Book(bookName, author, publisher, price);

// Book book = new Book("红楼梦", "曹雪芹", "人民文学出版社", 5);

book.note();


System.out.println("============================");

Book book2 = new Book("小李飞刀", "古龙", "中国长安出版社", 55.5);

book2.note();


System.out.println("============================");


// 无参构造

Book book3 = new Book();

book3.setPublisher("人民文学出版社");

book3.setPrice(5);

book3.note();


System.out.println("============================");

Book book4 = new Book();

book4.setPublisher("中国长安出版社");

book4.setPrice(55.5);

book4.note();


}


}


0

Michael_2020

提问者

2020-01-19

//这个是完善的代码。

//这里是Book类

package com.yito.book;

public class Book {


// 私有属性:书名、作者、出版社、价格

private String bookName;

private String author;

private String publisher;

private double price;


// 通过构造方法实现属性赋值

/*

* 通过公有的get/set方法实现属性的访问, 其中: 1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10 2、限定作者、书名均为只读属性

*/

public Book(String publisher, double price) {

this.bookName = "红楼梦";

this.author = "曹雪芹";

this.setPublisher(publisher);

this.setPrice(price);

}


public String getBookName() {

return bookName;

}


public String getAuthor() {

return author;

}


public String getPublisher() {

return publisher;

}


public void setPublisher(String publisher) {

this.publisher = publisher;

}


public double getPrice() {

return price;

}


public void setPrice(double price) {

if (price < 10) {

System.out.println("图书价格最低10元。");

this.price = 10;

} else

this.price = price;

}


// 信息介绍方法,描述图书所有信息

public void note() {

System.out.println("书名:" + this.bookName);

System.out.println("作者:" + this.author);

System.out.println("出版社:" + this.publisher);

System.out.println("价格:" + this.price);

}

}


//这里是测试类

package com.yito.book;


public class BookTest {


public static void main(String[] args) {

Book book = new Book("人民出版社", 5);

book.note();

System.out.println("============================");

//由于书名是在Book类中定义好的,而且只读,不知道怎么修改或定义。

Book book2 = new Book("中国长安出版社", 55.5);

book2.note();

}

}


0

Michael_2020

提问者

2020-01-19

//这里是Book类


package com.yito.book;

public class Book {

// 私有属性:书名、作者、出版社、价格

private String bookName;

private String author;

private String publisher;

private float price;

// 通过构造方法实现属性赋值

/*

* 通过公有的get/set方法实现属性的访问, 其中: 1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10 2、限定作者、书名均为只读属性

*/

public Book(String publisher, float price) {

this.bookName = "红楼梦";

this.author = "曹雪芹";

this.setPublisher(publisher);

this.setPrice(price);

}


public String getBookName() {

return bookName;

}


public String getAuthor() {

return author;

}


public String getPublisher() {

return publisher;

}


public void setPublisher(String publisher) {

this.publisher = publisher;

}


public float getPrice() {

return price;

}


public void setPrice(float price) {

if (price < 10) {

System.out.println("图书价格最低10元。");

this.price = 10;

} else

this.price = price;

}


// 信息介绍方法,描述图书所有信息

public void note() {

System.out.println("书名:" + this.bookName);

System.out.println("作者:" + this.author);

System.out.println("出版社" + this.publisher);

System.out.println("价格:" + this.price);

}


}


package com.yito.book;


//这里是测试类

public class BookTest {

public static void main(String[] args) {

Book book = new Book("人民出版社", 5);

book.note();


}


}


0

0 学习 · 11489 问题

查看课程