麻烦老师看下,这段代码有什么要修改的地方吗?谢谢

来源:1-7 编程练习

lankou

2019-07-23 13:14:10

public class Book {
      //私有属性:书名、作者、出版社、价格
         private String name;
         private String author;
         private double price;
         private String print;
      //通过构造方法实现属性赋值
      public Book(){
          
      }
      public Book(String name,String author,double price,String print){
       this.author=author;
       this.name=name;
       this.SetPrice(price);
       this.SetPrint(print);
      }

        /*通过公有的get/set方法实现属性的访问,其中:
        1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10
        2、限定作者、书名均为只读属性
        */
        public String getName(){
            return this.name;
        }
        public String getAuthor(){
            return this.author;
        }
        public String getPrint(){
            return this.print;
        }
        public void SetPrint(String print){
            this.print=print;
        }
         public double getPrice(){
            return this.price;
        }
        public void SetPrice(double price){
            if(price<10) {
                System.out.println("图书的价格最低10元!");
                this.price=10;
                }else {
            
            this.price=price;
            }
        }
       

      //信息介绍方法,描述图书所有信息
       public void show(){
            System.out.println("书名:"+this.getName());
            System.out.println("作者:"+this.getAuthor());
            System.out.println("出版社:"+this.getPrint());
            System.out.println("价格:"+this.getPrice());
       }


    }

public class BookTest {

    
        public static void main(String[] args) {
              //实例化对象,调用相关方法实现运行效果
              Book one=new Book("红楼梦","曹雪芹",8,"人民文学出版社");
              one.show();
              System.out.println("========================");
              Book one1=new Book("小李飞刀","古龙",55.5,"中国长安出版社");
              one1.show();
           
             }

    }

写回答

1回答

好帮手慕酷酷

2019-07-23

同学你好,在这段代码中,要注意方法名命名的规范问题,当方法或变量名由一个单词组成时,则该单词均小写。当由多个单词组成时,第一个单词所有字母均小写,从第二个单词开始,每个单词的首字母大写。其他的没有问题~

http://img.mukewang.com/climg/5d36b6e700018abd06080250.jpg

如果解决了你的疑惑,请采纳,祝学习愉快~ 


0

0 学习 · 11489 问题

查看课程