One, definition: Object-oriented, class is the smallest unit, interacting through classes
Cases:
First add a new class student, in this new class, write the rule
Namespace Mianxiangduixiang
{
Class student
{
//class can define member variables, the same as the structure of the body
public int code; //variables defined are generally called properties of the class
public string name; //public is a modifier that represents the scope of access
private string sex; //private indicates that only access to the student
public int Jiafa (int a)
{
return a+10;
}
}
}
And then refer to it in program.
Class Program
{
static void Main (string[] args)
{
Student s = new student (); //Create an object of student class S, initialize with new
S.code = 101; //Assign values to an object's properties
S.name = "Zhang San";
int A=s.jiafa (5); //Call method by Object
Student s2 = new Student ();
S2.name = "John Doe";
Console.WriteLine (s2.name); //Value printing
}
}
Determine whether an adult is based on the age of the object
1. First in the Student.cs:
public bool Panduan (student sdata)
{
if (sdata.nianling >=18)
{
return true;
}
Else
{
return false;
}
2. Then in program:
Student s3 = new Student (); //defines a S3
s3.nianling = 17;
BOOL B = S3.panduan (S3); //Send S3 to Sdata, let Sdata=s3
Console.WriteLine (b);
Write a function, the input parameter is an int array, sort descending and then return
Public int[] Paixu (int[] shuzu)
{
int count = Shuzu. Length;
for (int m = 0; m < count-1; m++)
{
for (int n = m; n < count; n++)
{
if (Shuzu[m] < shuzu[n])
{
int Zhong;
Zhong = shuzu[m];
SHUZU[M] = Shuzu[n];
Shuzu[n] = Zhong;
}
}
}
return Shuzu;
}
static void Main (string[] args)
{
int[] Shuzu = new INT[10];
for (int m = 0; m < m++)
{
SHUZU[M] = Int. Parse (Console.ReadLine ());
}
Console.WriteLine ("After sorting:");
Student s = new student ();
Shuzu = S.paixu (Shuzu);
foreach (int x in Shuzu)
{
Console.WriteLine (x);
}
Console.ReadLine ();
}
Second, the package:
How to use the mouse: right-click → refactor → encapsulate fields
public int Code
{
Get {return code;} //Read accessor
Set {Code=value;} //write accessor
}
private string name;
private string sex;
public string nianling
{
Get {return nianling;} //Read accessor
Set { //You can add a condition to the set
if (value. LENGTH<=10)
{Nianling=value;}
Else
{
Nianling= "does not meet the conditions";
}
Object-oriented programming in the 2015-01-12