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

来源:互联网 发布:不要网络的单机游戏 编辑:程序博客网 时间:2024/05/22 00:39

一、引用传递:

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


二、应用

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、一个职员的所属部门和MgrSystem.out.println(ea.getDept().getInfo());System.out.println("\t|-" + ea.getMgr().getInfo());System.out.println("\n------------------------------------------------------------------------------\n");//2、一个部门的所有职员信息及MgrSystem.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());}}}