请问老师 我代码中的错误 怎样修改

请问老师 我代码中的错误 怎样修改

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

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕小班 2019-07-17 17:48:04

        同学你好,请同学参考如下代码,修改自己的代码哦!

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();
	   
	    }
}

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

好帮手慕小班 2019-07-17 15:57:31

        同学你好,1、information方法报错的原因是大括号的问题哦!在java中不允许方法中嵌套定义方法哦!

http://img1.sycdn.imooc.com//climg/5d2ed3680001bbb008590424.jpg

    2、getPrice方法有一个double类型的返回值,所以这里要return一个double值哦!

综上所述 ,修改建议如下;

    http://img1.sycdn.imooc.com//climg/5d2ed44d0001c86908360445.jpg

    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方法,这个方法是没有返回值的,所以直接调用就可以了,不用放在输出语句中

http://img1.sycdn.imooc.com//climg/5d2ed6a400016a5f06280081.jpg

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


慕婉清0129613 2019-07-17 10:38:21

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());

   

    }

}


  • 提问者 yabun #1
    老师 我information那里报错是什么原因
    2019-07-17 14:29:56
  • 好帮手慕小班 回复 提问者 yabun #2
    同学你好,老师在上面的回答中回复了你这个问题! 继续加油~ 祝:学习愉快!
    2019-07-17 16:07:09
  • 提问者 yabun #3
    您好,代码修改之后,information那里报错是什么原因?
    2019-07-17 17:29:16
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师