请问老师 我代码中的错误 怎样修改
public class Book {
//私有属性:书名、作者、出版社、价格
private String name;
private String auther;
private String press;
private double price;
//通过构造方法实现属性赋值
public Book(String name,String auther){
this.name=getName();
this.auther=getAuther();
}
public String getName(){
return name;
}
public String getAuther(){
return auther;
}
public String setPress(String press){
return press;
}
public void getPress(String press){
this.press=press;
}
public double setPrice(double price){
return price;
}
public double getPrice(){
if(price<10){
System.out.println("图书价格最低10元");
price=10;
}
else{
this.price=getPrice();
}
public void information(){
System.out.println("书名:"+this.getName());
System.out.println("作者:"+this.getAuther());
System.out.println("出版社:"+this.setPress(press));
System.out.println("价格:"+this.setPrice(price)+"元");
}
}
}
--------------------------------------------------------
public class BookTest {
// 测试方法
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book book1=new Book("红楼梦","曹雪芹");
Book book2=new Book("小李飞刀","古龙");
book1.getPress("人民文学出版社");
book2.getPress("中国长安出版社");
book1.setPrice(5);
book2.setPrice(55.5);
System.out.println(book1.information());
System.out.println("==========================");
System.out.println(book2.information());
}
}正在回答 回答被采纳积分+1
同学你好,请同学参考如下代码,修改自己的代码哦!
public class Book {
// 私有属性:书名、作者、出版社、价格
private String name;
private String auther;
private String press;
private double price;
// 通过构造方法实现属性赋值
public Book(String name, String auther,String press,double price) {
this.name = name;
this.auther = auther;
this.press=press;
this.setPrice(price);
}
public String getName() {
return name;
}
public String getAuther() {
return auther;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
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 information() {
System.out.println("书名:" + this.getName());
System.out.println("作者:" + this.getAuther());
System.out.println("出版社:" + this.getPress());
System.out.println("价格:" + this.getPrice() + "元");
}
}public class BookTest {
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book book1=new Book("红楼梦","曹雪芹","人民文学出版社",5);
Book book2=new Book("小李飞刀","古龙","中国长安出版社",55.5);
book1.information();
System.out.println("==========================");
book2.information();
}
}如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
同学你好,1、information方法报错的原因是大括号的问题哦!在java中不允许方法中嵌套定义方法哦!

2、getPrice方法有一个double类型的返回值,所以这里要return一个double值哦!
综上所述 ,修改建议如下;

3、这里对价格的判断和修改不建议直接写在了get方法中,因为直接获取数据时,输出修改后的值,这样获取就是一个假数据,因为这个对象中存入的价格还是小于10的,是不符合条件的!所以这里建议将数据的判断写入放在set方法中哦!
4、在set/get方法中,set是写方法,例如:
public void setPress(double press) {
this.press=press;
}将传入的press参数写入对象中的press属性!
对应的get方法,将对象中的属性读取返回出来,例如:
public String getPress(String press) {
return this.press;
}其他属性参考这个修改!
5、在测试方法中调用information方法,这个方法是没有返回值的,所以直接调用就可以了,不用放在输出语句中

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
public class Book {
//私有属性:书名、作者、出版社、价格
private String name;
private String auther;
private String press;
private double price;
//通过构造方法实现属性赋值
public Book(String name,String auther){
this.name=getName(); //此处为赋值所以应使用setName改为:this.setName(name);
this.auther=getAuther();//同理改为:this.setAuther(auther);
}
public String getName(){
return name;
}
public String getAuther(){
return auther;
}
public String setPress(String press){
return press;//此处为赋值而不是返回值改为this.press=press;
}
public void getPress(String press){ //get得到,应该返回值public String getPress(){
this.press=press;//改为return this.press
}
//你对set和get不太明白,字面意思:set设置即给变量赋值,无需返回所以用void;get得到,得到变量的值返回即return
//所以以下set和get用混了
public double setPrice(double price){ //改为public double getPrice()
return price;//this.price=price;
}
public double getPrice(){ //public void setPrice(double price){
if(price<10){
System.out.println("图书价格最低10元");
price=10;
}
else{
this.price=getPrice();
}
public void information(){
System.out.println("书名:"+this.getName());
System.out.println("作者:"+this.getAuther());
System.out.println("出版社:"+this.getPress(press));
System.out.println("价格:"+this.getPrice(price)+"元");
}
}
}
--------------------------------------------------------
public class BookTest {
// 测试方法
public static void main(String[] args) {
//实例化对象,调用相关方法实现运行效果
Book book1=new Book("红楼梦","曹雪芹");
Book book2=new Book("小李飞刀","古龙");
book1.getPress("人民文学出版社");
book2.getPress("中国长安出版社");
book1.setPrice(5);
book2.setPrice(55.5);
System.out.println(book1.information());
System.out.println("==========================");
System.out.println(book2.information());
}
}
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星