using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
int a=8;
static void Main(string[] args)
{
Console.WriteLine("{0} ",a); //这个是错误的,静态的方法(这里指Main)是不能访问动态的普通变量的
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
test.function();
}
}
class test {
int y = 2;
public static int x = 3;
public void function(){
Console.WriteLine("{0} ",x+y);//这儿是对的,动态的方法是可以访问普通变量和静态变量的
}
}
}
静态的是属于类的, 而动态的是属于对象。
举个例子:狗都能叫,这就是类的方法。所以是静态的。当是,一个具体的对象,也就是一只具体的狗,也当然能叫啦,这就是动态为什么能调静态。 狗的毛发颜色都不一样吧,如果毛发颜色这个属性是静态的话,那么你要指定毛发颜色的值是什么。你连具体哪只狗都没指定,怎么知道它的毛发颜色呢。
所以静态方法里面是不能有动态属性的。
来自http://wenwen.soso.com/z/q220953854.htm