我想请问一下书名和作者为什么没法直接调用设置的get方法 还有出版社不是只读是不是需要设置set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package book; public class BookTest { // 测试方法 public static void main(String[] args) { //实例化对象,调用相关方法实现运行效果 Book one= new Book( "红楼梦" , "曹雪芹" , "人民文学出版社" , 9.0 ); one.show(); System.out.println( "===========================" ); Book two= new Book( "小李飞刀" , "古龙" , "中国长安出版社" , 55.5 ); two.show(); } } |
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 56 57 | package book; public class Book { //私有属性:书名、作者、出版社、价格 private String title; private String author; private String press; private double price; //通过构造方法实现属性赋值 public Book(){ } public Book(String title,String author,String press, double price){ this .title=title; this .author=author; this .setPress(press); this .setPrice(price); } /*通过公有的get/set方法实现属性的访问,其中: 1、限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10 2、限定作者、书名均为只读属性 */ public String getTitle(){ return this .title; } public String getAuthor(){ return this .author; } public void setPress(String press){ this .press=press; } public String getPress(){ return this .press; } public void setPrice( double price){ if (price< 10 ){ System.out.println( "图书价格最低10元" ); this .price= 10 ; } else { this .price=price; } } public double getPrice(){ return this .price; } //信息介绍方法,描述图书所有信息 public void show(){ System.out.println( "书名:" +title); System.out.println( "作者:" +author); System.out.println( "出版社:" +press); System.out.println( "价格:" +price+ "元" ); } } |
1
收起
正在回答 回答被采纳积分+1
2回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧