Java任务:建立图书对象

来自CloudWiki
跳转至: 导航搜索

任务描述

  • 图书是图书管理系统中最重要的商品。
  • 所有图书都具有书名、编号、作者、分类、库存、出版社、出版日期等属性。
  • 本任务要求使用Java语言建立“图书类”作为模板,描述图书作为商品的共有属性,并创建图书对象(实例)来管理每本图书的数据。
  • Java3-102.jpg

任务实现:建立图书对象

创建项目BookStore

创建包和类

  • 在包创建对话框中,输入包名 com.shangzhi.entity,点击 finish按钮,完成包的创建
  • 在包com.shangzhi.entity1上点击右键,选择菜单 New-Class 创建类
  • 输入类名Book,勾选红框内的复选框(包含main方法),点击Finish按钮完成Book类创建

为类添加成员变量

  • 在 Book类中插入代码,增加图书编号、名称、作者、库存数量、分类、价格成员变量,注意选择合适的数据类型
       String bid; // 书籍编号
	String name; // 书籍名字
	String author; // 书籍作者
	int number; // 书籍库存
        float price;//书籍价格
	String category; // 书籍详细分类(类目)

为类添加构造方法

  • 在 Book类中插入代码,增加包含空构造方法、包含6个参数的构造方法,方便将来创建图书对象
/*
	 * 空构造函数
	 */
	public Book() {
	}
	
	/*
	 * 含六个参数的构造函数
	 */
	public Book(String bid, String name, String author, int number, String category,float price) {
                
		this.bid = bid;//这里的this表示本类的bid属性,也可以不加
		this.name = name;
		this.author = author;
		this.number = number;
		this.category = category;
                this.price = price;
	}
  • 在 Book 类中插入代码,增加toString()方法,用于将图书信息转化为字符串,方便将来输出图书对象信息
@Override
	public String toString() {
 {
		return "商品编号:" + this.bid + " | 商品名:" + this.name + " | 类目:" + this.category + " | 商品总数:" + this.number+ " |商品价格:"+this.price;
 }

建立主类,创建对象

  • 在工程下面新建一个类MainClass ,在 该类的main 方法中插入代码,分别利用两个构造方法(无参和带参)创建图书对象b1和b2,并在控制台输出图书信息
public static void main(String[] args) {
		Book b1 = new Book();
		b1.bid = "b01";
		b1.name = "三国演义";
		b1.author = "罗贯中";
		b1.number = 40;
		b1.category = "小说";
		
		Book b2 = new Book("b02", "Java核心技术", "霍斯特曼", 50, "软件编程");

		System.out.println("图书 b1 " + b1);
		System.out.println("图书 b2 " + b2);
	}
}
  • 点击运行该程序,查看输出结果

完整代码

package entity;

public class Book {
	    String bid; // 书籍编号
	    String name; // 书籍名字
	    String author; // 书籍作者
	     int number; // 书籍库存
	    float price;//书籍价格
	    String category; // 书籍详细分类(类目)
		
		/*
		 * 空构造函数
		 */
		public Book() {
			
		}
		
		/*
		 * 含六个参数的构造函数
		 */
		public Book(String bid, String name, String author, int number, String category,float price) {
	                
			this.bid = bid;//这里的this表示本类的bid属性,也可以不加
			this.name = name;
			this.author = author;
			this.number = number;
			this.category = category;
	        this.price = price;
		}
		
		public  float getPrice(){
			return this.price;			
		}
		public void setPrice(float p){
			this.price=p;
		}
		public void ShowInfo() 
		 {
					
			 System.out.println( "商品编号:" + this.bid + " | 商品名:" + this.name 
					 +" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+ 
					 " |商品价格:"+this.price);
		}
		
		 @Override
		public String toString() 
		 {
				return "商品编号:" + this.bid + " | 商品名:" + this.name 
						+" | 作者:" + this.author+ " | 类目:" + this.category + " | 商品总数:" + this.number+
						" |商品价格:"+this.price+"\n";
		 }



	public static void main(String[] args) {
		Book b1 = new Book();
		b1.bid = "b01";
		b1.name = "三国演义";
		b1.author = "罗贯中";
		b1.number = 40;
		b1.category = "小说";
		
		Book b2 = new Book("b02", "Java核心技术", "霍斯特曼", 50, "软件编程",55.0f);

		System.out.println("图书 b1 " + b1);
		System.out.println("图书 b2 " + b2);
	}
}


返回 Java程序设计