学院首页>网络编程>ASP.NET>ASP.NET高级教程:对象(2)

ASP.NET高级教程:对象(2)

作者:bigeagle 来源:cndot.net 添加时间:2006-5-21 10:45:53
以上简单讲了一下如何定义类,看完这些内容,可能你就可以理解上一节我们构造的那个bbs对象,让我们再看一下它的定义: 


namespace MyOwnClass 



using System; 

using System.Data.SQL ; 

using System.Data ; 


//////////////////////////////////////////////////////////////////// 

// 

// Class Name : BBS 

// 

// Description: 论坛类,构造一个论坛对象 

// 

// date: 2000/02/03 

// 

/// //////////////////////////////////////////////////////////////// 

public class BBS 



//私有变量 

private string m_strTitle ; //bbs名称 

private int m_intForumCount ; //版面数 

private int m_intTopicCount ; //贴子数 

private int m_intUserCount ; //注册用户数 


//属性 

public string Title 



get 



return m_strTitle ; 






public int ForumCount 



get 



return m_intForumCount ; 






public int TopicCount 



get 



return m_intTopicCount ; 






public int UserCount 



get 



return m_intUserCount ; 






//构造函数 

public BBS(string a_strTitle) 



// 

// TODO: Add Constructor Logic here 

// 

m_strTitle = a_strTitle ; 


//读取数据库 

MyConnection myConn = new MyConnection() ; 

SQLCommand myCommand = new SQLCommand() ; 

myCommand.ActiveConnection = myConn ; 

myCommand.CommandText = "up_GetBBSInfo" ; //调用存储过程 

myCommand.CommandType = CommandType.StoredProcedure ; 


try 



myConn.Open() ; 

SQLDataReader myReader ; 

myCommand.Execute(out myReader) ; 

if (myReader.Read()) 



m_intForumCount = (int)myReader["ForumCount"] ; 

m_intTopicCount = (int)myReader["TopicCount"] ; 

m_intUserCount = (int)myReader["UserCount"] ; 



else 



throw(new Exception("表或存储过程不存在")) ; 




//清场 

myReader.Close(); 

myConn.Close() ; 



catch(SQLException e) 



throw(new Exception("数据库出错:" + e.Message)) ; 











和刚才我们讲的稍有不同,首先看第一行namespace MyOwnClass , 声明目前这个类的名字空间是MyOwnClass,名字空间就像一个包,其中可以包含很多类。再看这行: using System; 这个告诉编译器,我要引用System名字空间里的对象。然后其他的就好理解了吧?
站内搜索