2012年11月7日 星期三

[Android] Notification

Notification是指畫面上方資訊列的訊息提示

//先取出Notification管理員
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
//產生一個Notification
 Notification notification= new Notificatio( R.deawable.icon, "This is Message", System.currentTimeMills());
//建立Intent
Intent intent = new Intent(context,ActivityName.class);
//建立PendingIntent
 PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent ,0);

//設定 Notification內容
 notification.setLatestEventInfo(context,"This is Title","This is Message",pendingIntent);
//發送 Notification
 notificationManager.notify(0,notification);

2012年6月5日 星期二

[ActionScript3] get 與 set

AS3跟JAVA真的很像,當然get跟set的用法也類似
但還是有一些地方不同

get與set主要的用途是滿足封裝的概念
讓人無法知道以及隨意變更被封裝的函式與變數
get是用來取值,set是用來給值

 參考下面程式碼
public class Main extends Sprite 
{
  
 private var number:int=0; 
 public function Main():void 
 {

 }

 public function set Number(x:int):void
 {
  number = x;
 }

 public function get Number():int
 {
  return number;
 } 

}

由於number的private屬性,所以無法直接存取,需要使用其他方法來完成
而AS3中get跟set最大的特點就是
只用這兩個關鍵字的函式會被當成實體而不是函式

使用的方式直接如同一邊變數下去使用就可以了
var test:Main=new Main;

test.Number=100; //將number設成100
trace(test.Number); //取出number的值並印出

2012年6月3日 星期日

[ActionScript3] enum in AS3

在ActionScript3裡面沒有enum這個東西可以直接用
不過可以透過宣告靜態(static)與常數(const)來達成我們要的效果

下面舉一個水果盤的列舉集合來作例子
public class SYNBOL_DEFINE
{
        static public const SYM_BLUE_7  :uint = 0; // 藍 7
        static public const SYM_RED_7  :uint = 1; // 紅 7
        static public const SYM_BAR_3  :uint = 2; // BAR 3
        static public const SYM_BAR_2  :uint = 3; // BAR 2
        static public const SYM_BAR_1  :uint = 4; // BAR 1
        static public const SYM_MELON  :uint = 5; // 西瓜
        static public const SYM_BELL  :uint = 6; // 葡萄
        static public const SYM_MANGO  :uint = 7; // 芒果
        static public const SYM_ORANGE  :uint = 8; // 橘子
        static public const SYM_CHERRY  :uint = 9; // 櫻桃
        static public const SYM_I  :uint = 10; // i 寶寶
        static public const SYM_G  :uint = 11; // G
        static public const SYM_JOKER  :uint = 12; // Joker
}

當宣告static代表這個物件是屬於類別而不是實體
所以在使用的時候要使用類別名稱而不是實體名稱
var simple:SYNBOL_DEFINE=new SYNBOL_DEFINE;

simple.SYM_BLUE_7;          // 會產生錯誤
SYNBOL_DEFINE.SYM_BLUE_7    //正確用法


而宣告const的代表他是常數,是無法被變更的

2012年6月2日 星期六

[C/C++] 列舉(enum)


列舉主要的應用是在增加程式的可讀性
尤其是大型多人專案
 

以下舉個小例子來說明:

今天我們寫了一個ShowWeapon的函式

public void ShowWeapon(int x)
{
  cout << "your weapon is"
  switch(x)
  {
    case:1
      cout << "Axe";
      break;
    case:2
      cout << "Staff";
       break;
    case:3
      cout << "Dagger";
      break;
  }
}

當我們使用
ShowWeapon(1);    // point "your weapon is Axe"
給他1它會印出Axe 2就是Staff 3就是Dagger

在寫程式的當下會很爽
但是等你過幾天回來或是這個程式給別人維護的時候就會很不爽了

天殺的,誰知道你的代入值1,2,3是什麼意思

這個時候我們在前面加上enum的話會變成

enum Weapon : int
{
  Axe = 1,
  Staff = 2,
  Dagger = 3
}

public void ShowWeapon(int x)
{
  cout << "your weapon is"
  switch(x)
  {
    case:1
      cout << "Axe";
      break;
    case:2
      cout << "Staff";
       break;
    case:3
      cout << "Dagger";
      break;
  }
}

原本的ShowWeapon就可以這麼用
ShowWeapon(Weapon:Axe);     // point "your weapon is Axe"
ShowWeapon(Weapon:Staff);   // point "your weapon is Staff"
ShowWeapon(Weapon:Dagger);  // point "your weapon is Dagger"

可以讓自己也讓後面維護的人清楚這個位置的代入值要填入些什麼
也可以約束填入的內容才不會發生未預期到的錯誤

以上面的例子來說,如果沒有使用enum的話,很可能會出現4,5,6這類超出原本預期的數值
而使用enum後,把所有可以填的數值都規範在enum結構裡了