大神,帮我看下这段代码,不理解只读属性的概念。
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.mathod; public class Book { private String name; private String author; private String company; private double price; public Book(String name,String author,String company, double price) { this .setPrice(price); this .setCompany(company); this .name=name; this .author=author; } public String getCompany() { return company; } public void setCompany(String company) { this .company = company; } public double getPrice() { return price; } public void setPrice( double price) { if (price <= 10 ) { System.out.println( "最低价格不得低于10元" ); this .price = 10 ; } } public String getName() { return name; } public String getAuthor() { return author; } public void introduction() { System.out.println( "书名:" + this .getName()); System.out.println( "作者:" + this .getAuthor()); System.out.println( "出版社:" + this .getCompany()); System.out.println( "价格:" + this .getPrice()); } } |
作业要求作者和书名都是只读属性,根据课程讲的,只读属性意思是修改不了系统属性
那么this.name=name;
this.author=author;不就是直接修改系统属性了吗?而且还能任意填写信息。
29
收起
正在回答 回答被采纳积分+1
1回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧