C # design mode (class and object)

Source: Internet
Author: User

1. The difference between process-oriented programming and object-oriented programming lies in the appearance of classes.
2. The class is equivalent to a module, including private methods, public methods, attributes, and member data;

The following example clearly describes the Object-Oriented Design Philosophy:
Suppose we store the results of a swimming competition in a text data file. the operation we want to perform is to retrieve the data of each row from the text file and output the results of each row.
Design Concept:
1. Design a category for organizing athlete data, through which several attributes of athletes can be obtained.(For attributes such as name, age, club, and time, We must encapsulate them into a class called sort Mer. CS ).
2. when obtaining these attributes, we need to consider the space problem in each record, the time conversion problem, and the string segmentation problem (for each problem, we need to consider encapsulating it into a class called stringtokenizer. CS ).
3. When opening a file, you need to open the file and read one row at a time (this problem should be encapsulated into a class for file operations, called csfile. CS );

Csfile. CS class code:
Using system;
Using system. IO;

Namespace extends mertokenizer
{
/// <Summary>
/// A simple interface to file IO Methods
/// </Summary>
Public class csfile {
Private file FL;
Private streamreader ts;
Public csfile (string filename)
{
TS = file. opentext (filename );

}
Public String Readline (){
Return ts. Readline ();
}
Public void close (){
TS. Close ();
}
}
}

Code of the simmer. CS class:
Using system;
Using csharppats;

Namespace extends mertokenizer
{
/// <Summary>
/// Summary description for timer mer.
/// </Summary>
Public class extends mer
{
Private string frname, lname;
Private string Club;
Private int age;
Private int place;
Private formattime TMS;
//-----------
Public timer mer (string dataline ){
Stringtokenizer Tok = new stringtokenizer (dataline );
Place = convert. toint32 (Tok. nextelement (); // point to the next valid character
Frname = Tok. nextelement ();
Lname = Tok. nextelement ();
String S = Tok. nextelement ();
Age = convert. toint32 (s );
Club = Tok. nextelement ();
TMS = new formattime (Tok. nextelement ());
}
//-----------
Public string name {
Get {
Return frname + "" + lname;
}
}
//-----------
Public String time {
Get {
Return TMS. gettime ();
}
Set {
TMS = new formattime (value );
}
}
//-------------------
// Age Property
Public int age {
Get {
Return age;
}
Set {
Age = value;
}
}
}
}

Code for stringtokenizer. CS:
Using system;

Namespace csharppats
{
// This class is used to process string operations
// String tokenizer class
Public class stringtokenizer {
Private string data, delimiter;
Private string [] tokens;
Private int index;
Public stringtokenizer (string dataline ){
Init (dataline ,"");
}
Private void Init (string dataline, string delim ){
Delimiter = delim;
Data = dataline;
Tokens = data. Split (delimiter. tochararray ());
Index = 0;
}
Public stringtokenizer (string dataline, string delim ){
Init (dataline, delim );
}
Public bool hasmoreelements (){
Return (index <(tokens. Length ));
}
Public String nextelement (){
String S = tokens [index ++];
While (S. Length <= 0) & (index <tokens. Length ))
S = tokens [index ++];
Return S;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.