C#小菜!关于set,get
C#小菜求助!关于set,get
namespace exam1
{
class Program
{
static void Main(string[] args)
{
NewButton m = new NewButton();
m.x = 2;
}
}
class NewButton : Button
{
public int x
{
set
{
x = value;
}
get
{
return x;
}
}
}
}
请问一下为什么这里不可以直接给m.x赋值?一直都会提示错误
------解决思路----------------------
------解决思路----------------------
你这么干会造成死循环,每次设置x,都会调用set,然后set中 又会对x赋值,又会循环调用set
先设置一个局部变量,然后public时换一个名
------解决思路----------------------
namespace exam1
{
class Program
{
static void Main(string[] args)
{
NewButton m = new NewButton();
m.x = 2;
}
}
class NewButton : Button
{
public int x
{
set
{
x = value;
}
get
{
return x;
}
}
}
}
请问一下为什么这里不可以直接给m.x赋值?一直都会提示错误
------解决思路----------------------
int x;
public int X
{
set
{
x = value;
}
get
{
return x;
}
}
------解决思路----------------------
你这么干会造成死循环,每次设置x,都会调用set,然后set中 又会对x赋值,又会循环调用set
先设置一个局部变量,然后public时换一个名
------解决思路----------------------
private int _x;
public int X{
get{return _x;}
set{_x=value;}
}