asp.net static靜態變數

來源:互聯網
上載者:User

asp教程.net的頁面就是一個類,我們訪問一個頁面。就會在伺服器上執行個體化一個該類的執行個體,來響應我們的請求。
在asp.net教程 C#中,static靜態變數表示該變數屬於靜態類,而不是類的執行個體。可以說是該類的所有執行個體共用一個static變數。


先來看一個測試執行個體

#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;

long next();

int main() {
  for(int i = 0 ; i < 30 ; i++) {
    cout << std::setw(12) << next ();
  }
  cout << endl;
  return 0;
}

long next () {
  static long last = 0;
  last = last + 1;  
  return last;
}

輸出結果

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

Application與static變數
Application是通過一個集合儲存所有的對象。

強型別:
Application中儲存的是object,對對象的儲存和使用需要作cast動作。對於實值型別更需要Box&UnBox。對效能的影響較大。
而static變數是強型別的對象。

線程同步:
Application將所有的對象放到一個集合,這樣對訪問集合中的任何對象都會鎖定這個集合。
假如有Application["A"]、Application["B"]、Application["C"],有線程訪問Application["A"]其他線程不能訪問Application["B"] and Application["C"]。
而static變數,可以根據他們的作用分別放在不同的class當中。這樣可以並行訪問不同的static變數,而不存線上程安全問題。

 

使用靜態變數來計算,平均啟動並執行數字由使用者輸入

#include <iostream>
using namespace std;
 
int f(int i);
 
int main()
{
  int num;
 
  do {
    cout << "Enter numbers (-1 to quit): ";
    cin >> num;
    if(num != -1)
      cout << "average is: " << f(num) << "n";
   
  } while(num > -1);
 
  return 0;
}
 
int f(int i)
{
  static int sum = 0, count = 0;
 
  sum = sum + i;
 
  count++;
 
  return sum / count;
}

結果是

Enter numbers (-1 to quit): 1
average is: 1
Enter numbers (-1 to quit): 2
average is: 1
Enter numbers (-1 to quit): 3
average is: 2
Enter numbers (-1 to quit): 2
average is: 2
Enter numbers (-1 to quit): 1
average is: 1
Enter numbers (-1 to quit): 2
average is: 1
Enter numbers (-1 to quit): 3
average is: 2
Enter numbers (-1 to quit): -1

在類外初始化靜態成員欄位聲明

#include <iostream>
using std::cout;
using std::endl;

class Box {
  public:
    Box() {
      cout << "Default constructor called" << endl;
      ++objectCount;
      length = width = height = 1.0;
    }

    Box(double lvalue, double wvalue, double hvalue) :
                              length(lvalue), width(wvalue), height(hvalue) {
      cout << "Box constructor called" << endl;
      ++objectCount;
    }

    double volume() const {
      return length * width * height;
    }


    int getObjectCount() const {return objectCount;}

  private:
    static int objectCount;
    double length;
    double width;
    double height;
};
int Box::objectCount = 0;

int main() {
  cout << endl;

  Box firstBox(17.0, 11.0, 5.0);
  cout << "Object count is " << firstBox.getObjectCount() << endl;
  Box boxes[5];
  cout << "Object count is " << firstBox.getObjectCount() << endl;

  cout << "Volume of first box = "
       << firstBox.volume()
       << endl;

  const int count = sizeof boxes/sizeof boxes[0];

  cout <<"The boxes array has " << count << " elements."
       << endl;

  cout <<"Each element occupies " << sizeof boxes[0] << " bytes."
       << endl;

  for(int i = 0 ; i < count ; i++)
    cout << "Volume of boxes[" << i << "] = "
         << boxes[i].volume()
         << endl;

  return 0;
}

結果為

Box constructor called
Object count is 1
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Object count is 6
Volume of first box = 935
The boxes array has 5 elements.
Each element occupies 24 bytes.
Volume of boxes[0] = 1
Volume of boxes[1] = 1
Volume of boxes[2] = 1
Volume of boxes[3] = 1
Volume of boxes[4] = 1

友情提示:
1. 對static變數,做lock時。可以通過lock(typeof(classname))來鎖定該變數所在的類的類型,達到線程同步的目的。
2. 由於Aplication,static member是全域變數,而我們是在多線程伺服器環境寫程式,對他們的使用需要注意安全執行緒的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.