超级菜鸟问题:非静态的字段、方法或属性“ConsoleApplication1.Program.x”要求对象引用

ucdosxp 2007-09-01 06:15:14
class way
{
public static Boolean Adone, Bdone;
}
class Program
{
Boolean x;
static void Main(string[] args)
{
x = way.Adone;
}
}
...全文
271 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ucdosxp 2007-09-01
  • 打赏
  • 举报
回复
懂了撒,谢谢
wuyi8808 2007-09-01
  • 打赏
  • 举报
回复
class way
{
public static bool Adone, Bdone;
}

class Program
{
bool x;
void Test(string[] args)
{
x = way.Adone;
}

static void Main(){}
}

这样也可以通过编译。
你的理解是不对的,还需要多学习。
关键是在静态的Main()中不能使用非静态字段。
wuyi8808 2007-09-01
  • 打赏
  • 举报
回复
x = way.Adone等于说是x引用Adone
但是x与Adone不为一个类型的,一个静态,一个非静态,不匹配所以不能引用

----------------------------------------------

不对。
ucdosxp 2007-09-01
  • 打赏
  • 举报
回复
谢谢了,我的那种说法对不对。
wuyi8808 2007-09-01
  • 打赏
  • 举报
回复
// 或者这样:

class way
{
public static bool Adone, Bdone;
}

class Program
{
bool x;
static void Main(string[] args)
{
(new Program()).x = way.Adone;
}
}
wuyi8808 2007-09-01
  • 打赏
  • 举报
回复
class way
{
public static bool Adone, Bdone;
}

class Program
{
static bool x;
static void Main(string[] args)
{
x = way.Adone;
}
}
ucdosxp 2007-09-01
  • 打赏
  • 举报
回复
是不是这样的,
x = way.Adone等于说是x引用Adone
但是x与Adone不为一个类型的,一个静态,一个非静态,不匹配所以不能引用
ucdosxp 2007-09-01
  • 打赏
  • 举报
回复
非要这样吗,如果象我这样是静态的呢
xiao_jun_0820 2007-09-01
  • 打赏
  • 举报
回复
非静态的的话,就实例话一个对象,然后在用对象来引用:
class way
{
public Boolean Adone, Bdone;
}
class Program
{
Boolean x;

static void Main(string[] args)
{
way a=new way();
x=a.Adone

}
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧