看ms的petshop3.0遇到的问题,下面是源码:
public abstract class AddressUI : System.Web.UI.UserControl {
//...
                  //...
                  //... // Page property to set or get the address
public AddressInfo Address {
get {
// Make sure we clean the input
string firstName = WebComponents.CleanString.InputText(txtFirstName.Text, 50);
string lastName  = WebComponents.CleanString.InputText(txtLastName.Text, 50);
string address1  = WebComponents.CleanString.InputText(txtAddress1.Text, 50);
string address2  = WebComponents.CleanString.InputText(txtAddress2.Text, 50);
string city      = WebComponents.CleanString.InputText(txtCity.Text, 50);
string state     = WebComponents.CleanString.InputText(listState.SelectedItem.Text, 2);
string zip       = WebComponents.CleanString.InputText(txtZip.Text, 10);
string country   = WebComponents.CleanString.InputText(listCountry.SelectedItem.Text, 50);
string phone     = WebComponents.CleanString.InputText(txtPhone.Text, 10); return new AddressInfo(firstName, lastName, address1, address2, city, state, zip, country, phone);
}
set {
txtFirstName.Text = value.FirstName;
txtLastName.Text  = value.LastName;
txtAddress1.Text  = value.Address1;
txtAddress2.Text  = value.Address2;
txtCity.Text      = value.City;
txtZip.Text       = value.Zip;
txtPhone.Text     = value.Phone;
listState.SelectedItem.Value = value.State;
listCountry.SelectedItem.Value = value.Country;
}
}
下面是访问抽象类AddressUI的非抽象属性的源码:
protected AddressUI addr; 
AddressInfo address = addr.Address;下面是我的疑问:
addr不是只是一个声明吗?并没有分配内存空间啊,为什么可以访问到属性Address呢?

解决方案 »

  1.   

    代码就几行了,下面是全部
    using PetShop.Model;
    using PetShop.BLL;
    using PetShop.Web.Controls;
    //using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;namespace PetShop.Web {
    public class CreateAccount : Page { // Assume a duplicate record is the reason for the error
    private const string MSG_FAILURE = "Duplicate user ID! Please try again."; protected TextBox txtUserId;
    protected TextBox txtPassword;
    protected TextBox txtEmail;
    protected RequiredFieldValidator valUserId;
    protected AddressUI addr; 
    protected NavBar header;
    protected System.Web.UI.WebControls.RequiredFieldValidator valPassword;
    protected System.Web.UI.WebControls.RequiredFieldValidator valEmail;
    protected System.Web.UI.WebControls.ImageButton btnSubmit;
    protected System.Web.UI.HtmlControls.HtmlForm frmCreateAcct;
    protected Preferences prefs; protected void SubmitClicked(object sender, ImageClickEventArgs e) {
    if (Page.IsValid) { string userId = WebComponents.CleanString.InputText(txtUserId.Text, 50);
    string password = WebComponents.CleanString.InputText(txtPassword.Text, 50);
    string email = WebComponents.CleanString.InputText(txtEmail.Text, 50);
    AddressInfo address = addr.Address;
    string language = prefs.Language;
    string favCategory = prefs.Category;
    bool showFavorites = prefs.IsShowFavorites;
    bool showBanners = prefs.IsShowBanners; // Store all the customers information in an account business entity
    AccountInfo accountInfo = new AccountInfo(userId, password, email, address, language, favCategory, showFavorites, showBanners); ProcessFlow.AccountController accountController = new ProcessFlow.AccountController(); if (!accountController.CreateAccount(accountInfo)){ // Tell the user they have failed to create an account
    valUserId.ErrorMessage = MSG_FAILURE;
    valUserId.IsValid = false;
    }
    }
    }
    }
    }
      

  2.   

    public class CreateAccount : Page { // Assume a duplicate record is the reason for the error
    private const string MSG_FAILURE = "Duplicate user ID! Please try again."; protected TextBox txtUserId;
    protected TextBox txtPassword;
    protected TextBox txtEmail;
    protected RequiredFieldValidator valUserId;
    protected AddressUI addr; 
    //CreateAccount.aspx 会在被第一次访问是被编译为一个继承于 CreateAccount 类的子类,在
    那个子类里会实例华AddressUi 实例
      

  3.   

    感谢hdt(倦怠)的回答
    我大概明白你的意思,AddressUi和普通的服务器控件是相同的处理方式
    但是AddressUi被声明为:public abstract class AddressUI : System.Web.UI.UserControl 
    为什么抽象类可以实例化呢??
    实在还有点不解.
    照这样说,ms为什么要用抽象类呢,用具体类不可以了?水平太菜,困扰啊...
      

  4.   

    哈哈,我理解了啊.
    public abstract class AddressUI : System.Web.UI.UserControl 
    是个用户控件
    在CreateAccount.aspx类里会实例华AddressUI对应的AddressUI.ascx类,并不是AddressUI类
    而类AddressUI.ascx类是AddressUI的子类,
    所以和abstract 类AddressUI 不能实例化并没有冲突,是我一开始想不明白
    有提高了一步,嘿嘿,顶者有分...