老师,可以帮我看一下那里错误了吗

来源:1-7 编程练习

Caesar_王

2020-02-26 10:46:28

package lessonWork3;

public class Book {
    //设置私有属性
    private String book;
    private String author;
    private String publisher;
    private int price;
    //利用get和set方法对实现对属性的访问
    public String getBook() {
        return book;
    }
    public String getAuthor() {
        return author;
    }
    public String getPublisher() {
        return publisher;
    }
    public int getPrice() {
        return price;
    }
    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public void setPrice(int prince) {
        if(price<10) {
            System.out.println("图书价格必须大于10");
            price=10;
        }
        this.price=price;
    }
    
    //设计构造函数为属性进行赋值
    public Book(String book,String author,String publisher,int price) {
        this.book=book;
        this.author=author;
        this.publisher=publisher;
        this.setPrice(price);
    }
    
    //创建两个个函数,用来信息介绍
    public void method1(){
        println("书名:"+this.getBook()+"\n作者:"+this.getAuthor()+"\n出版社:"+this.getPublisher()+"\n价格:"+this.getPrice());
    }
    
    public void method2(){
        println("书名:"+this.getBook()+"\n作者:"+this.getAuthor()+"\n出版社:"+this.getPublisher()+"\n价格:"+this.getPrice());
    }
    
}


package lessonWork3;

public class BookTest {
    
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        BookTest booktest1=new BookTest("红楼梦"," 曹雪芹");
        BookTest booktest2=new BookTest("小李飞刀","古龙");
        booktest1.setPublisher("人民文学出版社");
        booktest1.sePrice(9);
        booktest2.setPublisher("中国长安出版社");
        booktest2.sePrice(55.5);
        booktest1.method1();
        System.out.println("=======================");
        booktest2.method2();
        
    }

}


写回答

1回答

好帮手慕小脸

2020-02-26

同学你好,代码中的问题如下:

    Book类

 1、对于价格进行判断,括号里是prince。

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

2、打印语句不正确,要书写完整。

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

    BookTest类

1、要实例化Book对象,在BookTest中测试,而不是实例化测试类

    1)创建相对应的有参构造方法。

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

    2)实例化Book对象,并调用有参构造为此赋值http://img1.sycdn.imooc.com/climg/5e55ee3409a1763b06930260.jpg

同学定义的价格为int类型,而传入的值是小数,所以建议更换为float或double类型即可。

其他问题:

1、 包名应该所有字母均小写,如:lessonWork3应改为:lessonwork3

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

1
haesar_王
h 谢谢老师!我回去再重温一遍实例化对象!
h020-02-26
共1条回复

0 学习 · 11489 问题

查看课程