代码如下,请老师检阅一下,是否有需要修改的地方

代码如下,请老师检阅一下,是否有需要修改的地方

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.imooc.object;
 
public class Book {
    private String title;
    private String author;
     
    private String press;
    private double price;
    private double lowestPrice = 10;
     
    public Book(String title,String author,String press,double price) {
        this.title = title;
        this.author = author;
        this.setPress(press);
        this.setPrice(price);
    }
     
    public void introduction() {
        System.out.println("书名:" this.getTitle());
        System.out.println("作者:" this.getAuthor());
        System.out.println("出版社:" this.getPress());
        System.out.println("价格:" this.getPrice() + "元");
         
    }
     
    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 = lowestPrice;
        }else {
            this.price = price;
        }
    }
 
    public String getTitle() {
        return title;
    }
 
    public String getAuthor() {
        return author;
    }
 
}

public class BookTest {


public static void main(String[] args) {

// TODO Auto-generated method stub

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

one.introduction();

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

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

two.introduction();

}


}


正在回答 回答被采纳积分+1

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

2回答
懵轩 2018-04-28 22:56:04

public book()这个方法还是可以读写书的名称和作者,题目有要求这两个应该是只读哦

  • 没有吧,只要不写set方法就行了
    2018-04-29 15:05:34
  • 提问者 空指针异常null #2
    意思是修改名称和作者的权限吗,改成protected?
    2018-05-02 19:04:35
  • chrismorgen 回复 提问者 空指针异常null #3
    我们说的名称和作者两个属性为只读属性,是因为在Book类中只有get方法,而没有set方法,没有set方法的话就不会改变名称和作者两个属性的值。祝学习愉快~
    2018-05-02 19:15:58
chrismorgen 2018-04-19 10:04:50

同学完成的不错,继续加油,祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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