2008/07/29

C#构造函数的练习

值得注意到是,先在构造函数里定义私有成员变量,接着再给相应地定义公共方法及属性

新建web窗体,命名BookTest.aspx,其中BookTest.aspx不做修改

BookTest.aspx.cs的代码修改如下:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;


public partial class BookTest : System.Web.UI.Page

{

public class Book

{

private string title;

private string isbn;

private double price=100.00;


public Book(string NewTitle,string NewIsbn)

{

title = NewTitle;

isbn = NewIsbn;

}


public string TitleInfo

{

get

{

return "书名: " + title + "<br>" + "isbn: " + isbn ;

}

}


public double Price

{

get

{

return price;

}

set

{

price = value;

}

}

}


protected void Page_Load(object sender, EventArgs e)

{

Book HotBook = new Book(".Net 3.5 入门教程","8-321-56424-5");

Response.Write(HotBook.TitleInfo + "<br>价格: " + HotBook.Price + "<br><br>");


Book NewBook = new Book("Visual Studio 2008 入门教程","9-545-16545-8");

NewBook.Price = 128.00;

Response.Write(NewBook.TitleInfo+"<br>价格: "+NewBook.Price);

}

}


输出结果:

书名: .Net 3.5 入门教程

isbn: 8-321-56424-5

价格: 100



书名: Visual Studio 2008 入门教程

isbn: 9-545-16545-8

价格: 128

没有评论: