private int _maxValue;private int maxValue;
get{ return _maxValue;}
set{_maxValue=value;}_maxValue 和maxValue是怎么联系起来的,_maxValue=10;它怎么就知道去访问maxValue,然后把value去赋值给_maxValue.
当读的时候也是一样,怎么就知道从maxValue里去执行它的get方法.
get{ return _maxValue;}
set{_maxValue=value;}_maxValue 和maxValue是怎么联系起来的,_maxValue=10;它怎么就知道去访问maxValue,然后把value去赋值给_maxValue.
当读的时候也是一样,怎么就知道从maxValue里去执行它的get方法.
private int _maxValue;get{ return _maxValue;}
set{_maxValue=value;}
get{ return _maxValue;}
set{_maxValue=value;}至于为什么要这么呢?我觉得是:体现了面向对象的封装,细节隐藏等等.其他的我也不清楚了,建议你去问问比尔,或者看看面向对象设计方法吧!
当读的时候也是一样,怎么就知道从maxValue里去执行它的get方法.
他们之间没有联系_maxValue = 10与maxValue没有任何关系,只不过你去取maxValue的值的时候,他return的是你修改后的_maxValue的值。你可以在get和set中抛出异常观察你对_maxValue操作的时候他们有没有被执行。
set{_maxValue=value;}
那我感觉这样更合理一下.为什么要声明一个:public int maxValue;
get{ return _maxValue;}
set{_maxValue=value;}public int Value;
get{ return _maxValue;}
set{_maxValue=value;}楼上说没关系,哪我弄成这样的了,它从哪找,都执行吗.
get{ return _maxValue;}
set{_maxValue=value;}
private int maxValue//没有分号。
get{ return _maxValue;}
set{_maxValue=value;}
至于他怎么找,属性的定义语法本来就是这样,你写成这样是一样的效果private int maxValue get{ return _maxValue;} set{_maxValue=value;}
{
.get instance int32 Test.Test::get_MaxValue()
.set instance void Test.Test::set_MaxValue(int32)
} // end of property Test::MaxValue我发现你这么写public int maxValue;IL也会转换成大写..method public hidebysig specialname instance void
set_MaxValue(int32 'value') cil managed
{
// 代码大小 9 (0x9)
.maxstack 8
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.1
IL_0003: stfld int32 Test.Test::maxValue
IL_0008: ret
} // end of method Test::set_MaxValue这是Set方法,它怎么就知道去Set:Test.Text::maxValue这个值去,总哪得有点联系,把他们弄到一起吧,或者说谁谁得告诉他你是谁谁的属性.
如果是java里应该就是要自己实现的那种方法..
比如 private string _name;
public string Name
{
get{return _name;}
set{_name = value;}
}
public string get_Name()
{
return _name;
}
public strin set_Name(string value)
{
_name = value;
}
当C#编译器看到代码试图读取或者设置一个属性时,她实际上会产生对相应方法的一个调用..而这方法应该是我们不可见的.
private int _maxValue;
public int SetValue(int maxValue)
{
_maxValue = maxValue;
}
public void GetValue()
{
return _maxValue;
}在Vs.net的下個版本里,繼續簡化成了
public int maxValue {get; set;}
public class ItemNode
{ private string nodeNameField; private string nodeValueField; public ItemNode(string nodeNameField,string nodeValueField)
{
this.nodeNameField=nodeNameField;
this.nodeValueField=nodeValueField;
}
/// <res/>
public string NodeName
{
get
{
return this.nodeNameField;
}
set
{
this.nodeNameField = value;
}
} /// <res/>
public string NodeValue
{
get
{
return this.nodeValueField;
}
set
{
this.nodeValueField = value;
}
}
}
1.对于对象的用户,属性显示为字段,访问该属性需要完全相同的语法。
2. 对于类的实现者,属性是一个或两个代码块,表示一个 get 访问器和/或一个 set 访问器。当读取属性时,执行 get 访问器的代码块;当向属性分配一个新值时,执行 set 访问器的代码块。访问性:
1.不具有 set 访问器的属性被视为只读属性。
2.不具有 get 访问器的属性被视为只写属性。
3.同时具有这两个访问器的属性是读写属性。属性具有多种用法:
1.它们可在允许更改前验证数据;
2.它们可透明地公开某个类上的数据,该类(OO里面我们通常称为业务实体类)的数据实际上是从其他源(例如数据库)检索到的;
3.当数据被更改时,它们可采取行动,例如引发事件或更改其他字段的值。为什么属性具有如此的优点?因为属性本质上就是方法!!!private int _maxValue;pubic int maxValue;
get{ return _maxValue;}
set{_maxValue=value;}实际C#编译器会生成类似如下的代码private int _maxValue;pubic int get__maxValue() { return _maxValue; }
public void set__maxValue(int value) { _maxValue=value; }
class.maxValue=this.txt1.text
{
get{ return _maxValue;}
set{_maxValue=value;}
}
是这样写的 set与get是访问器
你去看看书吧 基础入门写得很清楚的
set{_maxValue=value;}你这样会堆栈溢出。
这个value就是set的一个参数。