1-7编程有没有标准答案来参考一下啊,理解下大家的构思

1-7编程有没有标准答案来参考一下啊,理解下大家的构思

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
public class Book {
      //私有属性:书名、作者、出版社、价格
        private String bookName;
        private String author;
        private String pubName;
        private double price;
      //通过构造方法实现属性赋值
        public Book(double price,String bookName,String author,String pubName){
            this.price = setPrice(price);
            this.bookName = setBookName(bookName);
            this.author = author;
            this.pubName = pubName;
             
        }
        /*通过公有的get/set方法实现属性的访问,其中:
        1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
        2、限定作者、书名均为只读属性
        */
        public String setBookName(String bookName){
            return this.bookName =bookName;
        }
        public String getBookName(){
            return this.bookName;
        }
        public String getAuthor(){
            return this.author;
        }
        public String getPubName(){
            return this.pubName;
        }
        public double setPrice(double price){
            if(price<10.0){
                System.out.println("图书最低价格10元");
                this.price = 10;
            }else{
                this.price =price;   
            }
            return this.price;
        }
        public double getPrice(){
            return this.price;
        }
      //信息介绍方法,描述图书所有信息
        public void bookInfo(){
            System.out.println("书名:"+getBookName());
            System.out.println("作者:"+getAuthor());
            System.out.println("出版社:"+getPubName());
            System.out.println("价格:"+getPrice());
        }
 
    }
1
2
3
4
5
6
7
8
9
10
11
public class Test {
 
    public static void main(String[] args) {
         Book bk1 =new Book(5,"红楼梦","曹雪芹","人民文学出版社");
         bk1.bookInfo();
         System.out.println("===============================");
         Book bk2 =new Book(55.5,"小李飞刀","古龙","中国长安出版社");
         bk2.bookInfo();
    }
 
}

这是我自己写的代码,想看看大家写的,没有个标准答案来参考,总感觉自己写的不对

正在回答

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

2回答

代码没有标准答案,实现代码的方式有很多种。你的代码写的没有问题,只不过咱念作业中要求是把书名和作者设为只读属性,而你是把出版社和作者设置为了只读属性。同学写的book类很好。在test类中写的也正确,继续加油。祝学习愉快。

  • 摘星楼主 提问者 #1
    蛤,看错需求了,就记得有两个要求只读了......
    2018-06-18 14:24:02
提问者 摘星楼主 2018-06-17 00:48:52

求大神帮我看看怎么精简一下,对于代码复用的理解不深,不知道是test类中语句少写好,还是book类中少写点好。。。

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

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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