c#里对象的一个问题

cy527300280 2009-06-02 11:37:21
public partial class 基础_obj_this : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
book b1=new book("ASP.NET",24.4f);
book b2 = new book("XML", 22f);
book[] arr = new book[] { b1,b2 };
//sinfo s = new sinfo("张三", "男", 20, arr);
sinfo s2 = new sinfo("张三", "男", 20, arr);
Response.Write(s2.info());
}
}
class sinfo
{
private string name;
private string sex;
private uint age;
private book[] b;
public sinfo(string n, string s, uint a, book[] b)
{
this.age = a;
this.sex = s;
this.name = n;
this.b=b;
}
public string info()
{
string all = "";
foreach (book t in b)
{
all += t.bookinfo;

}

return "姓名:" + this.name + "<br>性别:" + this.sex + "<br>年龄:" + this.age + "<br>书籍信息:" + all;
}
}
class book
{
private string bookn;
private float price;
public book(string bn,float p)
{
this.bookn = bn;
this.price = p;
}
public string bookinfo()
{
return this.bookn+","+this.price;
}
}


请问下:
1、 foreach (book t in b)
{
all += t.bookinfo;

}
这个t.bookinfo是什么啊,我知道b是传过来的arr数组,t是book类型的,如果直接用 all+=t,结果输出来是 book book
2、这个book是类,怎么实例化后加个[]就成了数组了
...全文
60 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zgke 2009-06-02
  • 打赏
  • 举报
回复
all是事件...事件是可以委托

当第1次执行all+=t.bookinfo; 的时候

all[0] ==> b[0].bookinfo;
all[1] ==> b[1].bookinfo;

all里有两个方法..当all被除法的时候 会全部调用.

110,557

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧