12-11 Object-oriented-encapsulation, constructors

Source: Internet
Author: User

Iii. properties of Classes--encapsulation fields

Consists of two methods:

1. Read accessor: Get (take value)

2. Write accessor: Set (assigned value)

= = Package = =
private string _nihao;//The original ordinary variable private, by right---refactoring--encapsulation field implementation;
/*{
Get{return _name;} By default there are always get and set two methods, but the characteristics of information are hidden all the time;
Set{_name = value;}
}*/
Encapsulate Fields
public string name//the property after encapsulation.
Equivalent to the _name of the messenger of the intermediary, the package will be the original hidden variable properties displayed, two methods: Get,set.
{
Get// read accessor, fetch, return value of _name, procedure of value
{
Return _nihao;//return is the value of itself
}
Set// write accessor, setting the value of Name assignment to _name, the process of assigning a value
{
int sum = 0;
for (int i = 1; i <=; i++)
{
sum = sum + i;
}
Value = Sum. ToString ();
_nihao = value;

}

Called in the main function:

= = Package = =
FengZhuang p = new FengZhuang ();//must initialize new to invoke
Console.WriteLine ("Input:");
P.name = Console.ReadLine ();
Console.WriteLine (P.name);
Console.ReadLine ();

This will return the value.

Four, the constructor function

Definition: When an object is created with the New keyword, a special function is called automatically , which is the constructor of the class.

In C #, the constructor of a class is the same as the class name, and there is no return value.

Class student

{

Public student ()//constructor name of the same name, when the main function is called, as long as the initialization of new, the constructor can be automatically called.

{

Method

}

}

Exercise

///Student course, cycle through student information, output average score, highest score, lowest score, sort; (using object-oriented)
Console.Write ("Please enter student number:");
int n = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter student number, name, score");
ArrayList arr = new ArrayList ();//define a collection,
for (int i = 0; i < n; i++)
{
Student s = new student ();// Each loop in the For loop must be reinitialized, or the previous assignment will be replaced, with the last assignment
Console.Write ("study number:");
S.code = Int. Parse (Console.ReadLine ());
Console.Write ("Name:");
S.name = Console.ReadLine ();
Console.Write ("Score:");
S.score = Decimal. Parse (Console.ReadLine ());
Arr. ADD (s);//The properties of the class are all placed in the collection
}
//for the total score;
Decimal sum = 0;
for (int i = 0; i < n; i++)
{
Student AVGs = (student) arr[i];//This step is to cast the collection arr to the student class.
Sum + = AVGs. score;//total score
}

//for nested Loops bubble sort, highest score, Min.
for (int i = 0; i < n; i++)
{
for (int j = i; J < N; j + +)
{
Student S 1 = (student) arr[i];//the collection to the Student class by index and assigns the values in the collection to S1;
Student s2 = (student) arr[j];
if (S1. Score < S2. Score)//depending on the score, arrange
{
Student zhong = new Student () in the order of first large and small,  // bubble sort (focus)
Zhong = s1;< br> Arr[i] = s2;//means to assign a larger value to the I index
arr[j] = zhong;
}
}
}

Print output
Console.WriteLine ("Total score: {0}", sum);
Console.WriteLine ("Average score: {0}", sum/n);
Student SS1 = (student) arr[0];
Console.WriteLine ("Name: {0} Highest score: {1}", SS1. Name,ss1. Score);
int m=n-1;
Student SS2 = (student) arr[m];
Console.WriteLine ("Name: {0} min: {1}", SS2. Name, SS2. Score);
Sort by score, for loop
Console.WriteLine ("The result list of this class is as follows: *****************************");
for (int i = 0; i < n; i++)
{
Student AVGs = (student) arr[i];
Console.WriteLine ("Rank number of name score");
Console.WriteLine ("{0} {1} {2} {3}", I+1,avgs. Code,avgs. Name,avgs. Score);
}
Console.ReadLine ();

12-11 Object-oriented-encapsulation, constructors

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.