概念问题
MultiView控件的4个CommandName字段。为按钮的CommandName属性赋值,能够实现视图导航。
请问字段代表什么,跟属性有什么不同
------解决方案--------------------沙发
------解决方案--------------------英文 "Field "翻译过来的。在翻译.net术语的时候,把 "Property "叫做“属性”。不同的场景有不同的叫法,例如OO根本没有Field概念,统统是“属性”。
Reflector上打印出的代码如下:
[ControlBuilder(typeof(MultiViewControlBuilder)), DefaultEvent( "ActiveViewChanged "), Designer( "System.Web.UI.Design.WebControls.MultiViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a "), ToolboxData( " <{0}:MultiView runat=\ "server\ "> </{0}:MultiView> "), ParseChildren(typeof(View)), AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class MultiView : Control
{
// Fields
private int _activeViewIndex;
private int _cachedActiveViewIndex;
private bool _controlStateApplied;
private static readonly object _eventActiveViewChanged;
private bool _ignoreBubbleEvents;
public static readonly string NextViewCommandName;
public static readonly string PreviousViewCommandName;
public static readonly string SwitchViewByIDCommandName;
public static readonly string SwitchViewByIndexCommandName;
// Events
[WebSysDescription( "MultiView_ActiveViewChanged "), WebCategory( "Action ")]
public event EventHandler ActiveViewChanged;
// Methods
static MultiView();
public MultiView();
protected override void AddParsedSubObject(object obj);
protected override ControlCollection CreateControlCollection();
public View GetActiveView();
internal void IgnoreBubbleEvents();
protected internal override void LoadControlState(object state);
protected virtual void OnActiveViewChanged(EventArgs e);
protected override bool OnBubbleEvent(object source, EventArgs e);
protected internal override void OnInit(EventArgs e);
protected internal override void RemovedControl(Control ctl);
protected internal override void Render(HtmlTextWriter writer);
protected internal override object SaveControlState();
public void SetActiveView(View view);
private void UpdateActiveView(int activeViewIndex);
// Properties
[DefaultValue(-1), WebSysDescription( "MultiView_ActiveView "), WebCategory( "Behavior ")]
public virtual int ActiveViewIndex { get; set; }
[Browsable(true)]
public override bool EnableTheming { get; set; }
private bool ShouldTriggerViewEvent { get; }
[PersistenceMode(PersistenceMode.InnerDefaultProperty), Browsable(false), WebSysDescription( "MultiView_Views ")]
public virtual ViewCollection Views { get; }
}
------解决方案--------------------public 机器 a; //Field
public 机器 b //Property
{
get{ .....}
set{......}
}
例如你可以先把b定义为field,过一段时间再改为property,调用b的客户程序不用修改,很方便。
很接近。field直接分配在数据堆上的存储空间。而Property其实是一或两段程序,在有些语言中用set_xxxxx、get_xxxxxx方法来实现,而.net比较自然,从field扩展为property的时候不用修改客户程序。