javaSE-Day5 引用传递(数据表与java类的转换)

一、引用传递:

同一块堆内存可以被不同的栈内存所指向,不同的栈内存可以对同一块堆内存进行内容的修改;


二、应用

eg1 雇员与部门

	class Dept {
		private String dname;
		private int dnum;
		private String loc;
		private Emp[] emp;


		public Dept(String dname, int dnum, String loc) {
			this.dname = dname;
			this.dnum = dnum;
			this.loc = loc;
		}


		public void setEmp(Emp[] emp) {
			this.emp = emp;
		}


		public Emp[] getEmp() {
			return this.emp;
		}


		public String getInfo() {
			return "dname: " + this.dname + ", the num: " + this.dnum + ", loc: " + this.loc;
		}
	}


	class Emp {
		private String ename;
		private int eno;
		private String job;
		private double sal;
		private Emp mgr;
		private Dept dept;


		public Emp(String ename, int eno, String job, double sal) {
			this.ename = ename;
			this.eno = eno;
			this.job = job;
			this.sal = sal;
		}


		public void setMgr(Emp emp) {
			this.mgr = emp;
		}


		public Emp getMgr() {
			return this.mgr;
		}


		public void setDept(Dept dept) {
			this.dept = dept;
		}


		public Dept getDept() {
			return this.dept;
		}


		public String getInfo() {
			return "ename: " + this.ename + ", no: " + this.eno + ", job: " + this.job + ", sal: " + this.sal;
		}
	}


	public class TestDemoT {
		public static void main(String args[]) {
			//一、设置公司基础数据
			Emp ea = new Emp("Smith", 4, "tri", 1400.00);
			Emp eb = new Emp("Jack", 5, "dao", 2000.00);
			Emp ec = new Emp("Ming", 15, "Loader", 5000.00);
			Dept dept = new Dept("Ilvdy", 200, "Est");
			dept.setEmp(new Emp[] {ea, eb, ec});
			//二、建立关系
			ea.setMgr(eb);
			eb.setMgr(ec);
			ea.setDept(dept);
			eb.setDept(dept);
			ec.setDept(dept);


			//三、输出数据
				//1、一个职员的所属部门和Mgr
			System.out.println(ea.getDept().getInfo());
			System.out.println("\t|-" + ea.getMgr().getInfo());
			System.out.println("\n------------------------------------------------------------------------------\n");
				//2、一个部门的所有职员信息及Mgr
			System.out.println(dept.getInfo());
			for(int i = 0; i < dept.getEmp().length; i++) {
				System.out.println("\t|-" + dept.getEmp()[i].getInfo());
				if(dept.getEmp()[i].getMgr() != null) {
					System.out.println("\t\t|-" + dept.getEmp()[i].getMgr().getInfo());
				}
			}
		}
	}

eg2 商品

	class Item {
		private String iname;
		private int id;
		private String log;
		private Subitem[] subitem;
		private Produce[] produce;


		public Item(String iname, int id, String log) {
			this.iname = iname;
			this.id = id;
			this.log = log;
		}


		public String getInfo() {
			return "name: " + iname + ", id " + id + ", log " + log;
		}


		public void setSubitem(Subitem[] subitem) {
			this.subitem = subitem;
		}
		
		public Subitem[] getSubitem() {
			return this.subitem;
		}


		public void setProduce(Produce[] produce) {
			this.produce = produce;
		}


		public Produce[] getProduce() {
			return this.produce;
		}
	}


	class Subitem {
		private String sname;
		private int id;
		private String log;
		private Item item;
		private Produce[] produce;


		public Subitem(String sname, int id, String log) {
			this.sname = sname;
			this.id = id;
			this.log = log;
		}
		
		public void setProduce(Produce[] produce) {
			this.produce = produce;
		}


		public String getInfo() {
			return "name: " + sname + ", id " + id + ", log " + log;
		}


		public Produce[] getProduce() {
			return this.produce;
		}


		public void setItem(Item item) {
			this.item = item;
		}


		public Item getItem() {
			return this.item;
		}


	}


	class Produce {
		private String name;
		private double price;
		private Item item;
		private Subitem subitem;


		public Produce(String name, double price) {
			this.name = name;
			this.price = price;
		}


		public String getInfo() {
			return "name " + name + ", price " + price;
		}


		public void setItem(Item item) {
			this.item = item;
		}


		public Item getitem() {
			return this.item;
		}


		public void setSubitem(Subitem subitem) {
			this.subitem = subitem;
		}


		public Subitem getSubitem() {
			return this.subitem;
		}
	}


	public class TestIsp{
		public static void main(String args[]) {
			Item item = new Item("Ilvd", 1, "He");
			Subitem sua = new Subitem("Ming", 3, "-");
			Subitem sub = new Subitem("Yu", 5, "-");
			Produce p1 = new Produce("F", 100.00);
			Produce p2 = new Produce("D", 200.00);


			item.setSubitem(new Subitem[] {sua, sub});
			item.setProduce(new Produce[] {p1, p2});
			sua.setProduce(new Produce[] {p1, p2});
			sub.setProduce(new Produce[] {p1, p2});


			System.out.println(item.getInfo());
			for(int i = 0; i < item.getSubitem().length; i++) {
				System.out.println("\t|- " + item.getSubitem()[i].getInfo());
			}


			System.out.println("\n-----------------------------------------------------------------------\n");
			
			System.out.println(sua.getInfo());
			for(int i = 0; i < sua.getProduce().length; i++) {
				System.out.println(sua.getProduce()[i].getInfo());
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值