首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

有个 疑问 关于 静态函数解决办法

2012-04-20 
有个 疑问 关于 静态函数有个 疑问 关于 静态函数:public class A{...public static A b{A c new A()..

有个 疑问 关于 静态函数
有个 疑问 关于 静态函数

public class A
{
  ...
  public static A b
  { 
  A c = new A();
  ....
  }
}

有点晕,静态函数名前 类名的出现 不大懂了 静态对象?
 给分析分析

[解决办法]
public class A
{
...
public static A b
{
A c = new A();
....
}
}

这只是一个自己实例化自己的东西嘛,全局唯一的。
逻辑关系倒腾倒腾就好了,呵呵。
其实不难理解,这个还可以修改一个

C# code
    public class A    {        public static A _This = null;        public static A This        {            get            {                if (_This == null)                    _This = new A();                return _This;            }        }        private static string _TestStr = "";        public string TestStr        {            get { return _TestStr; }            set { _TestStr = value; }        }        public void ConsoleStr()        {            Console.Write(TestStr);        }    }    class Program    {        static void Main(string[] args)        {            A.This.TestStr = "aaa";            A.This.ConsoleStr();            A a = new A();            a.ConsoleStr();            Console.ReadKey();        }    }
[解决办法]
[Quote=引用:]

[code=C#][/code]这种 "在静态函数中实例化自己并给类属性赋值" 的优点在哪呢?
[/Quote]

类似于全局变量,这就好像一个全局对象,任何地方来访问都是唯一的对象,不会因为你的实例而改变内容,也可以根据自己的需要来扩展成局部的应用,

热点排行