C # Objects and methods
First, related concepts:
1. Objects: Entities in the real world
2. Class: A collection of objects with similar properties and methods
3. Object-oriented Programming features: Encapsulation inheritance polymorphism
Definition and Grammar of class
1, definition class: Modifier class name Class members
A) Define class syntax:
Modifier Class class Name
{
Class Members
}
2. Access modifiers for class: public internal
A) Public: accessible domain is the program where the access is and any referenced program Access is unrestricted
Define syntax:
public class class name
{
Class Members
}
b) Internal: Accessible domain definition scope (default access modifier)
Grammar:
(internal) class name
{
Class Members
}
3. Class Members: data members and fields
A) Data members: Fields and Constants
Fields: Variables
Declaration: Type field Name
Cases:
code is as follows |
copy code |
Public Class Persion { public string name; } Class Test { static void Main (string[] args) { & nbsp; persion persion=new persion (); persion.name= "Kaka"; Console.WriteLine ("{0}", persion.name); } } |
b) Method members
Statement:
Modifier return value type method name (argument list)
{
Method body
}
Modifiers: such as: public, private, protected, internal
Return value type: If the method has no return value, use void
Cases:
The code is as follows |
Copy Code |
public void Method () { Console.riteline ("method statement"); } |
4. Access modifiers for Members: public, private, protected, internal
A) public: publicly owned members
(b) Private: Privately owned Members
c) Protected: protection of Members
D) Internal: internal Members
Cases:
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace ConsoleApplication2
{
public class Employee
{
private float sum;
public int day;
public float wage;
Define method output Payroll information
public void Show ()
{
sum = day * wage;
Console.WriteLine ("Working time: {0}, daily Salary: {1}, Total salary: {2}", day,wage,sum);
}
}
Class Program
{
static void Main (string[] args)
{
Employee Employee = new Employee ();
Employee.day = 20;
Employee.wage = 50;
Employee.sum: Inaccessible Because it is a private member
Call method Reality pay
Employee. Show ();
}
}
} |
Third, the instantiation object: The key word: New
Cases:
The code is as follows |
Copy Code |
? using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Namespace ConsoleApplication1
{
public class car
{
private string Carname;
private string Cartype;
private int price;
public string Carname
{
get {return carname;}
set {carname = value;}
}
public string Cartype
{
get {return cartype;}
set {Cartype = value;}
}
public int Price
{
get {return price;}
set {price = value;}
}
public void Action ()
{
Console.WriteLine ("A car named {0}, model is {1}, the price is: {2}", Carname,cartype,price);
}
}
Create an instance and Access fields and methods
Class Program
{
static void Main (string[] args)
{
Create an instance of the car class
Car vehicle = new car ();
Assigning a value to an instance
Vehicle. Carname = "Benz";
Vehicle. Cartype = "XZ001";
Vehicle. Price = 1000000;
Call method
Vehicle.action ();
}
}
}
|
C # Object Array sorting method
Sorting is one of the commonly used methods in programming, there are many kinds of sorting methods, the following describes a simple and effective sorting method, the code is as follows:
code is as follows |
copy code |
private bool Isreverse = false; private void Sort (personalnotificationentity [] list,string key) { if (isreverse) { Array.reverse (l IST); Isreverse = false; } Else { int len = list. Length; Type type = typeof (Personalnotificationentity); Object [] keys = new Object[len]; for (int i = 0; i < len; i++) { Keys[i] = type. InvokeMember (Key,bindingflags.getfield, null,list[i],null); } Array.Sort (keys,list); Isreverse = true; } } |
Here we use the Array.Sort () and Array.reverse () methods to sort the data forward/reverse, and the variable isreverse as a bit of the reverse ordering
Method passes in 2 parameters, one is the array list of objects to sort, and one is the Sort keyword key, which is the property or field to which the object is to be sorted (this value is equal to the object's property/field name)
Type. The InvokeMember () method can get the property/field value of an object instance, where the field is used
After each field value in the array is obtained, the array of field values is passed as an argument to the Array.Sort () method, and the sort method sorts the number of objects by the value of the field.
Four, attributes
1,
A) Concept: a member of a field used to access a class
b attribute purpose: To ensure data security for data validation
2, the statement:
Access modifier data Type property name
{
get{}
set{}
}
3, get accessor
A) Meaning: To specify the value of a field without parameters, typically using the return statement, which returns the value of a variable to be invoked automatically when the property is evaluated
b The use of Get accessors:
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace ConsoleApplication2 { public class Employee { private string name = "slight"; public string Name { Get { Console.WriteLine ("program accesses get Accessor"); return name; } } } Class Program { static void Main (string[] args) { Employee Employee = new Employee (); Console.WriteLine (before "Access attribute"); Console.WriteLine (); Console.WriteLine (employee. Name); Console.WriteLine (); Console.WriteLine (after "Access attribute"); } } } |
4, set accessor: return value type is void
5. Property Type:
A read/write: Properties with Get () and set () accessors
B Read only: Have get (0 accessor properties, provide read to members)
c) write only (not recommended): contains only set () accessors
V. Parameters of the method
1. Value parameter: Pass by value
Cases:
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace ConsoleApplication2
{
public class Test
{
public void method (int x,int y)
{
int temp = x;
x = y;
y = temp;
Console.WriteLine ("Before Exchange: X={0},y={1}", x,y);
}
}
Class Program
{
static void Main (string[] args)
{
int a = 2;
int b = 5;
Test test = new test ();
Test. Method (A,B);
Console.WriteLine ("After Exchange: X={0},y={1}", a,b);
}
}
} |
2, reference parameters: Pass the argument to the method in memory address, delivered by address
Cases:
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace ConsoleApplication2
{
public class Test
{
public void method (ref int x,ref int y)
{
int temp = x;
x = y;
y = temp;
Console.WriteLine ("Before Exchange: X={0},y={1}", x,y);
}
}
Class Program
{
static void Main (string[] args)
{
int a = 2;
int b = 5;
Test test = new test ();
Test. Method (ref a,ref B);
Console.WriteLine ("After Exchange: X={0},y={1}", a,b);
}
}
} |
3. Output parameter: Pass back a result from method
Keywords: out
Cases:
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace ConsoleApplication2
{
public class Test
{
public void method (int x,out int y)
{
y = x + x;
}
}
Class Program
{
static void Main (string[] args)
{
Test test = new test ();
int outy;
Test. Method (Outy);
Console.WriteLine ("Y={0}", outy);
}
}
} |
4, the array parameter: parameter is only allowed to be a set of arrays, when the parameters of the method preceded by the params keyword, is a method with array parameters (using reference to pass)
Cases:
The code is as follows |
Copy Code |
Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Namespace ConsoleApplication2
{
public class Test
{
public void method (params int[] num)
{
Console.WriteLine ("Array size: {0}", Num.) Length);
foreach (int i in num)
{
Console.Write ("{0}", i);
}
Console.WriteLine ();
}
}
Class Program
{
static void Main (string[] args)
{
Int[] Nums = {1, 2, 3, 4, 5};
Test test = new test ();
Test. Method (Nums);
Test. Method (2,3,4,5);
Test. Method ();
}
}
} |
Supplemental object method attributes have and differ
In Object-oriented, objects and classes are different, objects are an instance of a particular class, for example if a car is a class, a Mercedes Benz is an object, and it is an example of a car. The class is abstract, and the object is concrete. A method is defined as an action on an object, an attribute is a variable that records the nature and state of the object, taking the example of the car above, the vehicle's weight, the maximum speed is the property of the car, starting, stopping in these actions can be defined as the method of the car. I said may not be very accurate, suggest the landlord to look at object-oriented related books.
Add:
Objects and classes are certainly not the same, and objects are specific to the class (not really accurate), for example,
Tell you the cat is a class that contains two attributes, weights and fur colors,
According to the above information, can you tell which cat is it? No, because you don't know its weight and fur color. Now instantiate the cat, that is, to specify its weight and fur color, assumed to be 1kg, black, and this 1kg black cat is the object, the same, 2kg white cat, 3kg yellow cat, etc. are objects.
Of course the 1kg Black Cat can also be used as a class to add a host attribute to this class,
Instantiate the class to get to like, for example Dick (1kg black Cat), John (1kg Black cat) ... Is the object of this class.
Then, Dick's lkg Black Cat can also be a class, and the object of this class, like the above, adds an attribute that can be distinguished.
......
This creates a hierarchy of classes, and then the concepts of the parent class, the subclass (the derived class), the inheritance, etc. are understandable.