AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > .NET技术

asp.net高级教程(三)---对象

51自学网 http://www.51zixue.net

 

这是我们可以改写一下刚才那个程序,运行结果同刚才一样:

public static void Main(String[] args)
{
MyClass myClass = new myClass() ; //构造MyClass类的一个实例
Console.WriteLine(myClass.Title) ; //结果是:我已经被赋初值了
myClass.MyMethod( "我的值改变了" ); //改变Title属性的值
Console.Writeline(myClass.Title) ; //这时结果变成:我的值改变了
}

以上简单讲了一下如何定义类,看完这些内容,可能你就可以理解上一节我们构造的那个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名字空间里的对象。然后其他的就好理解了吧?

 
 

上一篇:asp.net高级教程(四)---实战篇  下一篇:asp.net高级教程(二)--- 转换编程思维