当前位置:编程学习 > C#/ASP.NET >>

asp.net static静态变量

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是全局变量,而我们是在多线程服务器环境写程序,对他们的使用需要注意线程安全的问题

补充:asp.net教程,.Net开发 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,