sw.writeline("lastname:"+_lastname.tostring());
sw.writeline("phone:"+_phonenumber.tostring());
sw.flush();
sw.close();
fs.close();
}
}
public class extendedprofile:profile 建立profile子类extendedprofile,他可以继承profile中的方法
{
protected string _address1; //子类extendedprofile的属性
protected string _address2;
protected string _city;
protected string _state;
protected string _postal;
protected string _description;
public extendedprofile() //子类extendedprofile中属性的初始值
{
_address1 = "清华大学";
_address2 = "清华大学物理实验室";
_city = "北京";
_state = "北京";
_postal = "100024";
_description = "教授";
}
public override void setphonenumber(string phonenumber) //继承类profile中的setphonenumber()方法
{ //setphonenumber()方法的重载
_phonenumber = phonenumber;
}
public string getaddress1() //子类extendedprofile中的方法getaddress1(),以下类推
{
return _address1;
}
public string getaddress2()
{
return _address2;
}
public void setaddress(string address1,string address2)
{
_address1 = address1;
_address2 = address2;
}
public string getcity()
{
return _city;
}
public void setcity(string city)
{
_city = city;
}
public string getstate()
{
return _state;
}
public void setstate(string state)
{
_state = state;
}
public string getpostal()
{
return _postal;
}
public void setpostal(string postal)
{
_postal = postal;
}
public string getdescription()
{
return _description;
}
public void setdescription(string description)
{
_description = description;
}
public override void save() //调用接口isavedata()中的方法save(),save()方法的重载,由于多态性
{ //子类extendedprofile可以自定义并修改save()方法
string _document = "d:\\myweb2\\saidy.xml";
xmltextwriter writer = null; //保存为一个xml文件
try
{
writer = new xmltextwriter(_document,null);
writer.formatting = formatting.indented;
writer.writestartdocument(false);
writer.writedoctype("profile",null,null,null); //表示<!doctype profile>
writer.writestartelement("profile"); //生成根元素
writer.writeelementstring("firstname",_firstname); //生成子元素 <firstname>_firstname</firstname>
writer.writeelementstring("lastname",_lastname);