2015年10月30日 星期五

自動產生hashCode與equals

hashCode與equals 重要嗎??
老實說有時候感覺是個累贅.
但有時候你重要到你不能忽視!~
當你在寫自己的物件時,若物件會被丟進HashMap、HashSet、HashTable中
那hashCode與equals是必然要的。
我知道有人會逃避...因為書上寫得很複雜..
但其實不用..
交給Eclipse 自動產生就好
自動產生hashCode與equals
Eclipse自動產生hashCode與equals


 選擇要加入的屬性按下OK

2015年10月28日 星期三

列舉與Switch

package learning.test;
public class myenum1 {
public enum week{Sundau,Monday,Tuesday}
public enum Month{Sundau,Monday,Tuesday}
public static void main(String[] args){
 week ww=week.Monday;
 switch (ww){ // 若switch 對象型態為 week
 case Monday: // 則case只能是week 中定義的值, 也不能寫成week.Monday
  System.out.println("Monday");
  break;
 case Tuesday:
  System.out.println("Tuesday");
  break;
 default:
  break;
 }
 if(week.Monday.toString()=="Monday"){
 
 }
}
}