ASP.NET Advanced Tutorial (Continued)

zhaozj2021-02-08  235

Let me talk about how to construct a BBS object, there are friends asking me to introduce how to construct the object in C #, let's simply say it, it is enough to make up this lesson.

Class C #, can also be called objects (Object), which consists of the following parts: member variables, attributes, and methods, which are essential that this class does not have a constructor for any parameters, it does not specify back Type, the role is the member variable of the initialization class, allocated memory, etc. Unlike C , C # classes only constructor, do not need the pavilion function, that is, you only need to assign memory for member variables, not the explicit release of memory, because C # and Java are all passed through the garbage collector To release memory. Understand these, we can construct a simple class, one thing that does not do:

Public Class Myclass, PUBLIC CLASS MYCLASS

{

Public myclass () {};

}

It only needs a simple New one when you want to use, like this: myclass myclass1 = new myclass (); don't doubt, you have created an object, although it doesn't do anything :). Below we add a private character-type member variable m_strtitle, and initialize this member variable in the constructor, the entire class definition becomes like this:

Public Class Myclass, PUBLIC CLASS MYCLASS

{

// Private member variable

PRIVATE STRING M_STRTITLE;

//Constructor

Public myclass ()

{

m_strtitle = "I have already been assigned"; "

}

}

CAUTION Define this line code for member variables: private string m_strtitle; where String is well understood, explain that m_strtitle is a string type, then what is the most in front of the private? This private indicates that this member variable is private, only the functions of this class can be used, and any otherwhere includes a function of subclasses, in addition to Private, you can also define into public (public) and protected (Protection), public indicates that this member variable can be used anywhere, and protected indicates that this variable can only be used in this class or subclass. So, if we want to use this member variable, you can define it into public, but for object-oriented programming, this is not a good program habit, because it destroys the encapsulation of the class, the good method in C is Define public functions to access this private variable, and C # provides a more convenient way, which is property (now we add this property title plus:

Public Class Myclass, PUBLIC CLASS MYCLASS

{

// Private member variable

PRIVATE STRING M_STRTITLE;

//Attributes

Public String Title

{

get

{

Return this.m_strtitle;

}

set

{

THIS.M_STRTILE = VALUE;

}

}

//Constructor

Public myclass ()

{

m_strtitle = "I have already been assigned"; "

}

}

Let's take a look at how to define attributes, first we need a scope qualifier, usually we use public, indicating that you can use this property anywhere, followed by two keywords Need Note: this and value, this represents the class itself, so THIS.M_STRTITLE is a member variable M_STRTITLE that represents this class, Value represents the value of the right side of the left value as the left value, like this: myclass.title = "Hello", the value of Value is "Hello", ok, This class is already available, like the following: public static void main (string [] args)

{

Myclass myclass = new myclass (); // Construct an instance of the MyClass class

Console.writeline (MyClass.title); // The result is: I have been assigned.

Myclass.title = "My value changed"; // change the value of the Title property

Console.writeline (MyClass.title); // At this time, the result is changed.

}

Ok, let us add a MyMethod method to this class, this method does not return a value, with a character type parameter.

Public Class Myclass, PUBLIC CLASS MYCLASS

{

// Private member variable

PRIVATE STRING M_STRTITLE;

//Attributes

Public String Title

{

get

{

Return this.m_strtitle;

}

set

{

THIS.M_STRTILE = VALUE;

}

}

//Constructor

Public myclass ()

{

m_strtitle = "I have already been assigned"; "

}

//method

Public void mymethod (String a_str)

{

THIS.M_STRTILE = A_STR;

}

}

This is what we can rewrite the program just now, and the results are just just the same:

Public static void main (string [] args)

{

Myclass myclass = new myclass (); // Construct an instance of the MyClass class

Console.writeline (MyClass.title); // The result is: I have been assigned.

Myclass.mymethod ("My value changed"); // change the value of the Title property

Console.writeline (MyClass.title); // At this time, the result is changed.

}

The above briefly tells how to define classes. After reading these content, you can understand the BBS object we constructed in the previous section, let's take another definition:

Namespace MyownClass

{

Using system;

Using system.data.sql;

Using system.data;

//

// Class Name: BBS

//

// DESCRIPTION: Forum class, construct a forum object

//

// Date: 2000/02/03

//

///

Public Class BBS

{

// Private variable

Private string m_strtitle; // bbs name

Private int m_intforumcount; // layout

PRIVATE INT m_INTTTOPICCOUNT; / / Post number private int m_intusercount; // Registered user number

//Attributes

Public String Title

{

get

{

Return M_STRTILE;

}

}

Public int forumcount

{

get

{

Return M_INTFORUMCOUNT;

}

}

Public int topiccount

{

get

{

Return M_INTTTOPICCOUNT;

}

}

Public int Usercount

{

get

{

Return M_IntuserCount;

}

}

//Constructor

Public BBS (String a_STRTITLE)

{

//

// Todo: add constructor logic here

//

m_strtitle = a_strtitle;

// Read the database

MyConnection myconn = new myconnection ();

Sqlcommand mycommand = new sqlcommand ();

MyCommand.activeConnection = myconn;

Mycommand.commandtext = "UP_GETBBSINFO"; // Call the stored procedure

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 ")));

}

//Clearance

MyReader.Close ();

MyConn.close ();

}

Catch (SQLException E)

{

Throw (New Exception);

}

}

}

}

And the slightly different from our threy, first look at the first line of Namespace Myownclass, the name space of this class is MyownClass, the namespace is like a package, which can contain many classes. Look at this line: use system; this tells the compiler, I want to reference the object in the SYSTEM name space. Then other other understand it?

转载请注明原文地址:https://www.9cbs.com/read-3481.html

New Post(0)