Java任务:建立商品对象

来自CloudWiki
Cloud17讨论 | 贡献2020年4月8日 (三) 10:21的版本 为类添加构造方法
跳转至: 导航搜索

任务描述

  • 餐品是商院面馆管理系统中最重要的商品。
  • 所有餐品都具有名称、价格、数量等属性。
  • 本任务要求使用Java语言建立“面条类”作为模板,描述面条作为商品的共有属性,并创建面条对象(实例)来管理系统中每碗面条的数据。
  • Ai2020-4-20.png

任务实现:建立商品对象

创建项目Noodles

创建包和类

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

public class Noodles {
		
	
	
	public static void main(String[] args) {        
        
	}

}

通过类创建对象

在main方法中通过类创建对象:

 Noodles n1;
 n1= new Noodles();//声明一个Noodle类的对象n1  

为类添加成员变量

  • 在Noodles类中插入代码,增加名称、价格、数量,注意选择合适的数据类型
       
public class Noodles {
	        //商品的名称
		public String name;
		//商品的价格
		public float price;
		//商品的数量,
		public int num;	

		public Noodles() {
			
		}
		
		public static void main(String[] args) {
			// TODO Auto-generated method stub
	                
	            //处理
	            Noodles n1= new Noodles();//声明一个Noodle类的对象n1
	              
				
		}

}

在main方法中读写这几个变量:

 

  public static void main(String[] args) {
                //处理
	        Noodles n1= new Noodles();//声明一个Noodle类的对象n1
	        n1.name=name;//给变量赋值
	        n1.price = 10;//给变量赋值
	        n1.num = num;//给变量赋值
	        
	        //输出
	        System.out.print("您点的是"+n1.name);//打印变量
	        System.out.print(" 价格:"+n1.price);//打印变量
	        System.out.println(" 数量"+ n1.num);//打印变量 	
}

为类添加构造方法

  • 在 Noodles类中插入代码,增加包含空构造方法、包含6个参数的构造方法,方便将来创建图书对象
    //空构造方法	
	public Noodles(){
		
    }
	public Noodles(String n) {
		this.name = n;
	}
	public Noodles(String n,float p) {
		this.name = n;
		this.price = p;
	}
	public Noodles(String n,float p,int num) {
		this.name = n;
		this.price = p;
		this.num = num;
	}


在main方法中尝试通过构造方法创建对象:

Noodles n2= new Noodles("老北京牛肉面",15.0f,3);
System.out.println(n2.toString());//打印变量 

添加成员方法

  • 在Noodles类中插入代码,增加成员方法getPrice和setPrice
        public float getPrice( ){
	    System.out.println("该商品的价格为 : " + price); 
	    return price;
	}
	public void setPrice(float p) {
		this.price =p;
	}

  • 在 Noodles 类中插入代码,增加toString()方法,用于将面品信息转化为字符串,方便将来输出面品对象信息
public String toString() {
		String str ="商品名称:"+this.name+ 
				    " 价格:"+this.price+
				    " 数量:"+ this.num;
		return str;
	}

建立主类,创建对象

  • 在工程下面新建一个类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程序设计