1. Related Concepts:
1. Objects: entities in the real world
2. Class: a set of objects with similar attributes and Methods
3. Features of object-oriented programming: encapsulation inheritance Polymorphism
Ii. Class Definition and syntax
1. Define class: modifier class name class member
A) Definition class Syntax:
Modifier class Name
{
Class Member
}
2. Class access modifier: public internal
A) public: The accessible domain is the program in which it is located and access to any referenced program is unrestricted.
Definition Syntax:
Public class name
{
Class Member
}
B) internal: within the defined range of accessible domains (default access modifier)
Syntax:
(Internal) class Name
{
Class Member
}
3. Class Members: data members and fields
A) data member: Field and constant
Field: Variable
Declaration: Type field name
Example:
public class Persion{ public string name;}class Test{ static void Main(string[] args) { Persion persion=new Persion(); persion.name="kaka"; Console.WriteLine("{0}",persion.name); }}
B) method Member
Statement:
Modifier return value type method name (parameter list)
{
Method body
}
Modifiers: Public, private, protected, and internal
Return Value Type: if the method does not return a value, use void
Example:
Public void method () {console. riteline ("method declaration ");}
4. Access modifiers for Members: Public, private, protected, and internal
A) Public: Public Member
B) Private: Private member
C) protected: protects members.
D) Internal: internal Member
Example:
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; // defines the method to output the salary information public void Show () {sum = day * wage; console. writeLine ("working hours: {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: unable to access because it is a private member // call method real wage employee. show ();}}}
Iii. instantiated object: Keyword: new
Example:
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: {1}, price: {2}", carName, carType, price );}} // create an instance and access fields and Methods class Program {static void Main (string [] args) {// create a car class instance car vehicle = new car (); // assign vehicle to the instance. carName = "Benz"; vehicle. carType = "XZ001"; vehicle. price = 1000000; // call the vehicle method. action ();}}}
Iv. Attributes
1,
A) Concept: Used as a member of the fields of the category class
B) attribute purpose: ensure data security for data verification
2. Statement:
Access modifier data type attribute name
{
Get {}
Set {}
}
3. Get accessors
A) meaning: this parameter is used to specify the value of a field to the outside. Generally, the return statement is used to return the value of a variable. It is automatically called when the attribute value is set.
B) Use of get accessors:
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace consoleapplication2 {public class employee {private string name = ""; Public string name {get {console. writeline ("the program has accessed the get accessors"); Return name ;}} class program {static void main (string [] ARGs) {employee Employee = new employee (); console. writeline ("before accessing properties"); console. writeline (); console. writeline (employee. name); console. writeline (); console. writeline ("after access property ");}}}
4. Set accessors: the return value type is void.
5. attribute type:
A) read/write: Attributes of get () and set () accessors
B) read-only: it has the get (0 accessors attribute and provides read to members.
C) Write-only (not recommended): contains only set () accessors
V. method parameters
1. Value parameter: pass by value
Example:
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 switching: 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 address of the real parameter in the memory to the method, and pass the parameter by address
Example:
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 ("after switching: x = {0}, y = {1}", x, y );
} Class Program {static void Main (string [] args) {int a = 2; int B = 5; Test test = new Test (); Console. writeLine ("before switching: x = {0}, y = {1}", a, B );
Test. Method (ref a, ref B );}}}
3. output parameters: pass a result from the method
Keyword: out
Example:
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(35, out outy); Console.WriteLine("y={0}",outy); } }}
4. array parameters: a parameter can only be a group of arrays. When a method parameter prefixed with the params keyword, it is a method with array parameters (passed by reference)
Example:
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 ();}}}