国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net中靜態變量和非靜態變量的區別

2024-07-10 13:13:10
字體:
來源:轉載
供稿:網友

靜態變量:
靜態變量使用 static 修飾符進行聲明
在所屬類被裝載時創建
通過類進行訪問
所屬類的所有實例的同一靜態變量都是同一個值
非靜態變量:
不帶有 static 修飾符聲明的變量稱做非靜態變量
在類被實例化時創建
通過對象進行訪問
同一個類的不同實例的同一非靜態變量可以是不同的值
示例:


[復制到剪貼板]code:

using system;
using system.collections.generic;
using system.text;
namespace example01
{
    class program
    {
        class class1
        {
            public static string staticstr = "class";
            public string notstaticstr = "obj";
        }
        static void main(string[] args)
        {
            //靜態變量通過類進行訪問,該類所有實例的同一靜態變量都是同一個值
            console.writeline("class1's staticstr: {0}", class1.staticstr);
            class1 tmpobj1 = new class1();
            tmpobj1.notstaticstr = "tmpobj1";
            class1 tmpobj2 = new class1();
            tmpobj2.notstaticstr = "tmpobj2";
            //非靜態變量通過對象進行訪問,不同對象的同一非靜態變量可以有不同的值
            console.writeline("tmpobj1's notstaticstr: {0}", tmpobj1.notstaticstr);
            console.writeline("tmpobj2's notstaticstr: {0}", tmpobj2.notstaticstr);
            console.readline();
        }
    }
}


結果:
class1's staticstr: class
tmpobj1's notstaticstr: tmpobj1
tmpobj2's notstaticstr: tmpobj2

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰州市| 贡觉县| 定陶县| 临夏市| 龙川县| 石嘴山市| 岑巩县| 镇坪县| 衡水市| 进贤县| 乌拉特前旗| 喀喇沁旗| 汉源县| 平和县| 积石山| 凯里市| 汉川市| 页游| 东光县| 常德市| 陈巴尔虎旗| 博湖县| 出国| 西宁市| 临夏市| 苏尼特左旗| 彝良县| 将乐县| 化州市| 东兰县| 泰州市| 辰溪县| 吉林省| 个旧市| 邵武市| 大埔县| 昌吉市| 忻城县| 定南县| 体育| 仪征市|