JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:[問]MAP觀念應用 [Re:ggininder]
frankiewu





發文: 11
積分: 0
於 2018-11-16 09:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
個人不建議濫用MAP,
因為程式未來不一定都是你在維護
是我的話寧願物件化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
//國小class
class elementary_chool {
    String SchoolName;
    ArrayList<class_detail> put_class=new ArrayList();
    public ArrayList<class_detail> getPut_class() {
        return put_class;
    }
 
    public void setPut_class(ArrayList<class_detail> put_class) {
        this.put_class = put_class;
    }
 
    public String getSchoolName() {
        return SchoolName;
    }
 
    public void setSchoolName(String schoolName) {
        SchoolName = schoolName;
    }
 
}
//國中class
class junior_high {
    String SchoolName;
 
    ArrayList<class_detail> put_class=new ArrayList();
    public ArrayList<class_detail> getPut_class() {
        return put_class;
    }
 
    public void setPut_class(ArrayList<class_detail> put_class) {
        this.put_class = put_class;
    }
 
    public String getSchoolName() {
        return SchoolName;
    }
    public void setSchoolName(String SchoolName) {
        this.SchoolName = SchoolName;
    }
 
}
//高中class
class Senior_High {
    
    String SchoolName;
 
    ArrayList<class_detail> put_class=new ArrayList();
    public ArrayList<class_detail> getPut_class() {
        return put_class;
    }
 
    public void setPut_class(ArrayList<class_detail> put_class) {
        this.put_class = put_class;
    }
 
    
    public String getSchoolName() {
        return SchoolName;
    }
    public void setSchoolName(String schoolName) {
        SchoolName = schoolName;
    }
 
}
class class_detail{
    String ClassName;
    int Studentnum;
    public  class_detail(String ClassName,int Studentnum){
        this.ClassName=ClassName;
        this.Studentnum=Studentnum;
    }
    public String getClassName() {
        return ClassName;
    }
 
    public void setClassName(String className) {
        ClassName = className;
    }
}
public class main implements test {
    public static void main(String args[]) {
        //建立國小物件
        elementary_chool elementary_chool = new elementary_chool();
        elementary_chool.setSchoolName("內湖國小");
        class_detail class1=new class_detail("一年一班",20);
        class_detail class2=new class_detail("一年二班",21);
        elementary_chool.getPut_class().add(class1);
        elementary_chool.getPut_class().add(class2);
        //建立國中物件
        junior_high junior_high = new junior_high();
        junior_high.setSchoolName("內湖國中");
        class1=new class_detail("二年一班",22);
        class2=new class_detail("二年一班",23);
        junior_high.getPut_class().add(class1);
        junior_high.getPut_class().add(class2);
        //建立高中物件
        Senior_High senior_high = new Senior_High();
        senior_high.setSchoolName("內湖高中");
        class1=new class_detail("三年一班",24);
        class2=new class_detail("三年二班",25);
        senior_high.getPut_class().add(class1);
        senior_high.getPut_class().add(class2);
        //產生(map)EducationEntiy   置入國小、國中、高中 物件
        Map<String, Object> EducationEntiy = new HashMap<String, Object>();
        EducationEntiy.put("國小", elementary_chool);
        EducationEntiy.put("國中", junior_high);
        EducationEntiy.put("高中", senior_high);
        //從EducationEntiy 取回物件
        elementary_chool show_elementary_chool = (elementary_chool) EducationEntiy.get("國小");
        junior_high show_junior_high = (junior_high) EducationEntiy.get("國中");
        Senior_High show_Senior_High = (Senior_High) EducationEntiy.get("高中");
        //國小內容物印出
        System.out.println(show_elementary_chool.getSchoolName());
        for (class_detail a:show_elementary_chool.getPut_class()
             ) {
            System.out.println("班級:"+a.ClassName+"人數:"+a.Studentnum);
        }
        //國中內容物印出
        System.out.println(show_junior_high.getSchoolName());
        for (class_detail a:show_junior_high.getPut_class()
        ) {
            System.out.println("班級:"+a.ClassName+"人數:"+a.Studentnum);
        }
        //高中內容物印出
        System.out.println(show_Senior_High.getSchoolName());
        for (class_detail a:show_Senior_High.getPut_class()
        ) {
            System.out.println("班級:"+a.ClassName+"人數:"+a.Studentnum);
        }
    }
}

執行結果:
內湖國小
班級:一年一班人數:20
班級:一年二班人數:21
內湖國中
班級:二年一班人數:22
班級:二年一班人數:23
內湖高中
班級:三年一班人數:24
班級:三年二班人數:25


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
347 [問]MAP觀念應用 ggininder 242 2018-11-16 01:22
288 Re:[問]MAP觀念應用 clock 1051 2018-11-16 08:25
272 Re:[問]MAP觀念應用 mmolis 360 2018-11-16 09:15
288 Re:[問]MAP觀念應用 frankiewu 4454 2018-11-16 09:58
267 Re:[問]MAP觀念應用 ggininder 221 2018-11-19 01:40
248 Re:[問]MAP觀念應用 clock 3462 2018-11-17 10:37
» JWorld@TW »  Java 新手區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8