数据表的转换
- 一对多
例:
class Privence{
private int pid;
private String pname;
private City city[];
public Privence(int pid,String pname){
this.pid = pid;
this.pname = pname;
}
public void setCity(City city[]){
this.city = city;
}
public City[] getCity(){
return city;
}
public String getinfo(){
return "省份ID:"+this.pid+"、省份名称:"+this.pname;
}
}
class City{
private int cid;
private String cname;
private Privence pvc;
public City(int cid,String cname){
this.cid = cid;
this.cname = cname;
}
public void setPvc(Privence pvc) {
this.pvc = pvc;
}
public Privence getPvc() {
return pvc;
}
public String getinfo(){
return "城市ID:"+this.cid+"、城市名称:"+this.cname;
}
}
public class day04 {
public static void main(String args[]){
Privence p = new Privence(01,"河南");
City c1 = new City(1001,"洛阳");
City c2 = new City(1002,"郑州");
c1.setPvc(p);
c2.setPvc(p);
p.setCity(new City[]{c1,c2});
System.out.println(c1.getPvc().getinfo());
System.out.println(c2.getPvc().getinfo());
System.out.println("--------------------");
for (int x =0;x < p.getCity().length;x++){
System.out.println(" "+p.getCity()[x].getinfo());
}
}
}
- 一对多
例:
private String tname; private int tyid;class Type{
private String tdetils;
private Goods gds[];
public Type(int tyid,String tname,String tdetils){
this.tyid = tyid;
this.tname = tname;
this.tdetils = tdetils;
}
public void setGds(Goods gds[]) {
this.gds = gds;
}
public Goods[] getGds() {
return gds;
}
public String getinfo(){
return "类型:"+this.tyid+"、类型名称:"+this.tname+"、描述:"+this.tdetils;
}
}
class Subtype {
private int stid;
private String stname;
private String stdetils;
private Type tid;
private Goods Gd[];
public Subtype(int stid, String stname, String stdetils) {
this.stid = stid;
this.stname = stname;
this.stdetils = stdetils;
}
public void setGd(Goods[] gd) {
Gd = gd;
}
public Goods[] getGd() {
return Gd;
}
public void setTid(Type tid) {
this.tid = tid;
}
public Type getTid() {
return tid;
}
public String getinfo() {
return "子类型ID:" + this.stid + "、子类型名称:" + this.stname + "、子类型描述:" + this.stdetils;
}
}
class Goods {
private int gid;
private String gname;
private double price;
private Type tid;
private Subtype sbid;
public Goods(int gid, String gname, double price) {
this.gid = gid;
this.gname = gname;
this.price = price;
}
public void setTid(Type tid) {
this.tid = tid;
}
public void setSbid(Subtype sbid) {
this.sbid = sbid;
}
public Type getTid() {
return tid;
}
public Subtype getSbid() {
return sbid;
}
public String getinfo() {
return "商品ID:" + this.gid + "、商品名称:" + this.gname + "、商品价格:" + this.price;
}
}
public class day05 {
public static void main(String args[]) {
Type t = new Type(01, "家具", "家具用品");
Subtype st = new Subtype(101, "厨房灶具", "厨房所用的物品");
Goods gd = new Goods(10101, "勺子", 20.0);
Goods gc = new Goods(10102, "菜刀", 50.0);
t.setGds(new Goods[]{gd,gc});
//厨房灶具是勺子和菜刀的子类型
gd.setSbid(st);
gc.setSbid(st);
//厨房灶具包含勺子和菜刀
st.setGd(new Goods[]{gd,gc});
//通过一个类型找到它所对应的全部商品,以及每个商品的子类型。
for (int x = 0; x < t.getGds().length; x++) {
System.out.println(t.getGds()[x].getinfo());
System.out.println(t.getGds()[x].getSbid().getinfo());
}
System.out.println("-----------------");
for (int x = 0; x < st.getGd().length; x++) {
System.out.println(st.getGd()[x].getinfo());
}
}
}
- 多对多