老师检查下,看看能不能优化下,另外有几个小问题

来源:1-7 编程练习

罗杰明

2019-11-14 11:07:55

class Book {
/**
* 私有变量属性
* @param name书名
* @param author作者
* @param press出版社
* @param price价格
*/
private String name;
private String author;
private String press;
private float price;
/**
* 创建无参构造方法
*/
public Book(){
name="小李飞刀";
author="古龙";
press="中国长安出版社";
price=55.5f;
System.out.println("书名:"+name+'\n'+"作者:"+author+'\n'+"出版社:"+press+'\n'+"价格:"+price);
}
/**
* 创建有参参构造方法
*/
public Book(float price){
this.setPrice(price);
}
/**
*创建出版社set/gei方法
*/
public void setPress(String press){
this.press=press;
}
public String getPress(){
return this.press;
}
/**
*创建价格set/get方法
*/
public void setPrice(float price){
if(price<=10f){
System.out.println("价格最低为10元");
this.price=10f;
}else{
this.price=price;
}
}
public float getPrice(){
return this.price;
}
/**
*创建价格只读属性
*/
public String getName(){
return "红楼梦";
}
public String getAuthor(){
return "曹雪芹";
}


public static void main(String[] args) {
//先调用有参构造方法(有参构造方法调用setPrice方法)
Book b1=new Book(9);
System.out.println("书名:"+b1.getName());
System.out.println("作者:"+b1.getAuthor());
b1.setPress("人民文学出版社");
System.out.println("出版社:"+b1.getPress());
System.out.println("价格:"+b1.getPrice());
System.out.println("=================================");
Book b=new Book();
}
}




问题1:IDEA中怎样快速创建set/get方法
问题2:this怎么理解,有什么作用

写回答

1回答

好帮手慕酷酷

2019-11-14

同学你好!整体完成不错,但是还有以下优化的空间:

1、建议创建一个测试类,将测试方法,也就是main()方法编写在测试类中。使代码结构清晰,每个类有每个类的功能。

2、根据题目,要求设计构造函数实现对属性赋值,建议同学创建有参构造方法,在创建对象时,传递属性值,为属性赋值,而不是在无参方法中直接为属性赋值。具体如下:

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

3、根据题目要求只是限定作者、书名均为只读属性,没有限定作者为只读属性,建议将anthor的set方法进行添加,并且作者、书名的get方法不要直接返回值,因为如果直接返回属性值,则当属性值更改时,还需要更改对应get方法的值。所以要返回当前对象的name属性值。

具体如下:

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

4、建议编写一个信息介绍方法,而不是编写在无参构造中。

5、测试类中直接创建带参对象,调用信息介绍方法就可以了

例如如下代码:

public class Book {
	//成员属性
	private String name;
	private String author;
	private String press;
	private double price;
	public Book() {
	}
	//带参构造方法
	public Book(String name,String author,String publishHouse,double price) {
		this.author=author;
		this.name=name;
		this.setPrice(price);
		this.setPublishHouse(publishHouse);
		
	}

	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=10.0;}
		else
			this.price = price;
	}

	public String getName() {
		return name;
	}

	public String getAuthor() {
		return author;
	}
	
	public void infoIntroduce() {
		System.out.println("书名:"+this.getName());
		System.out.println("作者:"+this.getAuthor());
		System.out.println("出版社:"+this.getPress());
		System.out.println("价格:"+this.getPrice());

	}
}

BookTest类:

public class BookTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book one = new Book("红楼梦","曹雪芹","人民文学出版社",1);
		one.infoIntroduce();
		System.out.println("================================");
		Book two = new Book("小李飞刀","古龙","中国长安出版社",55.5);
		two.infoIntroduce();
	}

}

6、idea中快速创建get、set方法的方式:

  • 鼠标右键选择Generate,如:

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

  • 点击"Getter and Setter", 

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

  • 将定义的字段全部选中,点击OK


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

7、this表示当前对象的含义,例如创建book对象,在进行调用有参构造时,通过this.name=name,就是指将有参构造中接收的name属性的值,赋值给当前对象book的name属性中。

8、建议同学可以再回顾一下《Java面向对象》2-6小节中对this关键字的讲解

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 11489 问题

查看课程