面向对象这章的最后给我们讲了枚举类,这个又让我想起了用c++的感觉。java中我还从来没用过,看了这书我才知道枚举类的方便。先看个不用枚举类实现枚举的例子,eg
- public class Season
- {
-
- private final String name;
- private final String desc;
- public static final Season SPRING = new Season("春天" , "趁春踏青");
- public static final Season SUMMER = new Season("夏天" , "夏日炎炎");
- public static final Season FALL = new Season("秋天" , "秋高气爽");
- public static final Season WINTER = new Season("冬天" , "围炉赏雪");
- public static Season getSeaon(int seasonNum)
- {
- switch(seasonNum)
- {
- case 1 :
- return SPRING;
- case 2 :
- return SUMMER;
- case 3 :
- return FALL;
- case 4 :
- return WINTER;
- default :
- return null;
- }
- }
-
- private Season(String name , String desc)
- {
- this.name = name;
- this.desc = desc;
- }
-
- public String getName()
- {
- return this.name;
- }
- public String getDesc()
- {
- return this.desc;
- }
- }
- public class TestSeason
- {
- public TestSeason(Season s)
- {
- System.out.println(s.getName() + ",这真是一个"+ s.getDesc() + "的季节");
- }
- public static void main(String[] args)
- {
-
- new TestSeason(Season.FALL);
- }
- }
现在用枚举,eg
- public class TestEnum
- {
- public void judge(SeasonEnum s)
- {
-
- switch (s)
- {
- case SPRING:
- System.out.println("春暖花开,正好踏青");
- break;
- case SUMMER:
- System.out.println("夏日炎炎,适合游泳");
- break;
- case FALL:
- System.out.println("秋高气爽,进补及时");
- break;
- case WINTER:
- System.out.println("冬日雪飘,围炉赏雪");
- break;
- }
- }
- public static void main(String[] args)
- {
-
- for (SeasonEnum s : SeasonEnum.values())
- {
- System.out.println(s);
- }
- new TestEnum().judge(SeasonEnum.SPRING);
- }
- }
方便了不少,最主要是思路清晰了。
然后讲下垃圾回收机制中的花样。本来我觉得垃圾回收是jvm的事情。现在才知道由垃圾回收引出了,强,软,弱,虚四种引用,软引用是当内存不足时回收,弱和虚差不多,相当于类没引用,只是虚引用的回收会被记录下来,可以跟踪对象的回收。
最后收尾讲了jar文件的使用,其实jar,war,ear都是用zip来压缩的,只是里面多点配置文件。命令的使用,我就不说了,这个网上很多。
这里我主要讲下如何打个可执行的包:
在jar包的manifest.mf中加入Main_Class:(空格)包名.类名(回车)
千万注意空格和回车不要忘了
本章习题
定义一个类,封装一桌梭哈游戏。
- public class ShowHand
- {
-
- private final int PLAY_NUM = 5;
-
-
- private String[] types = {"/4 " , "/5 " ,"/3 " , "/6 "};
- private String[] values = {"2" , "3" , "4" , "5" , "6" , "7" , "8" ,
- "9", "10" , "J" , "Q" , "K" , "A"};
-
- private List<String> cards = new LinkedList<String>();
-
- private String[] players = new String[PLAY_NUM];
-
- private List<String>[] playersCards = new List[PLAY_NUM];
-
- public void initCards()
- {
- for (int i = 0 ; i < types.length ; i++ )
- {
- for (int j = 0; j < values.length ; j++ )
- {
- cards.add(types[i] + values[j]);
- }
- }
-
- Collections.shuffle(cards);
- }
-
- public void initPlayer(String... names)
- {
- if (names.length > PLAY_NUM || names.length < 2)
- {
-
- System.out.println("玩家数量不对");
- return ;
- }
- else
- {
-
- for (int i = 0; i < names.length ; i++ )
- {
- players[i] = names[i];
- }
- }
- }
-
- public void initPlayerCards()
- {
- for (int i = 0; i < players.length ; i++ )
- {
- if (players[i] != null && !players[i].equals(""))
- {
- playersCards[i] = new LinkedList<String>();
- }
- }
- }
-
- public void showAllCards()
- {
- for (String card : cards )
- {
- System.out.println(card);
- }
- }
-
- public void deliverCard(String first)
- {
-
- int firstPos = ArrayUitls.search(players , first);
-
- for (int i = firstPos; i < PLAY_NUM ; i ++)
- {
- if (players[i] != null)
- {
- playersCards[i].add(cards.get(0));
- cards.remove(0);
- }
- }
-
- for (int i = 0; i < firstPos ; i ++)
- {
- if (players[i] != null)
- {
- playersCards[i].add(cards.get(0));
- cards.remove(0);
- }
- }
- }
-
- public void showPlayerCards()
- {
- for (int i = 0; i < PLAY_NUM ; i++ )
- {
-
- if (players[i] != null)
- {
-
- System.out.print(players[i] + " : " );
-
- for (String card : playersCards[i])
- {
- System.out.print(card + "/t");
- }
- }
- System.out.print("/n");
- }
- }
- public static void main(String[] args)
- {
- ShowHand sh = new ShowHand();
- System.out.println(Arrays.toString(sh.types));
- sh.initPlayer("电脑玩家" , "孙悟空");
- sh.initCards();
- sh.initPlayerCards();
-
- sh.showAllCards();
- System.out.println("---------------");
-
- sh.deliverCard("孙悟空");
- sh.showPlayerCards();
-
-
- sh.deliverCard("电脑玩家");
- sh.showPlayerCards();
- }
- }