1. Process-oriented <--> object-oriented
Process oriented: It is the process of accomplishing this thing, emphasizing the action of accomplishing it.
Object oriented: Find an object to help you do something. Intended to write a generic code that masks differences.
Everyone has a name, gender, height, weight, age. These can be seen as properties of objects
The object must be visible and palpable.
We further encapsulate these objects with the same properties and methods, abstracting out the concept of class.
A class is a mold that determines the properties and methods that an object should have.
Objects are created from classes.
2. Class
Grammar:
[Public] class name
{
field; (Store data, store multiple values)
Properties
Method
}
After writing a class, we need to create the object of this class,
The process of creating an object of this class is called instantiation of the class.
Use the keyword new.
This: Represents the object of the current class.
The class is not in memory, and the object is memory-occupied.
There are only classes, no objects, and when we instantiate an object and assign it a value (object), the object appears. The operation is then done for this object. This is an object-oriented operation.
Custom classes, which are not provided by the system, are written by themselves.
The field has an initial value by default. The field occupies memory.
3. Properties
The function of a property is to protect a field, assign a field, and qualify a value.
The nature of the property is two methods, one called Get () and one called set ().
√get () √set () Read writable property.
√get () Xset () read-only property
Xget () √set () write-only property
Fields field
Methods method
Property Properties
Set is to assign values passed in to the field by property.
An assignment is called when a value is given.
Use the two methods in the attribute to qualify the field.
When you assign a value to a property, the Set method is executed first.
The Get method is executed when you output a property that is worth it.
The properties in the procedure are not stored in value, just a transition, and then processing.
A field is a woman, and a property is a man.
A woman at home is a child, and all dealings with the outside world are made by men.
4. Objects
Once we have created an object of a class, we need to assign a value to each property of the object.
We call this process the initialization of the object.
Inside the set, the value of value is judged, and the value of the field is judged in get.
Set assigns the value assigned to the property to the field, and get returns the value of the field (a limit can be made before returning)
If the output field in the method does not output a property, the get judgment is not used, and there is no way to limit it.
5. Static and non-static differences
1), in a non-static class, there can be either an instance member or a static member. (Non-static fields, properties, methods and static fields, properties, methods)
2), when invoking instance members, you need to use the object name. Instance members;
When you call a static member, you need to use the class name. static member name;
get{return person._name;}
Set{person._name=value;}
Summary: Static members must be called using the class name, and instance members are invoked using the object name.
In a static function, only static members can be accessed and instance members are not allowed.
Instance functions, you can use either static members or instance members.
Only static members are allowed in static classes, and instance members are not allowed.
Q: Why do static classes not let you create objects?
A: Because it calls to use the class name to invoke, creating the object is useless, not used, not syntactically.
Use:
1), if you want your class to be used as a "tool class", consider writing the class as static. Often used, as in the console
2), static classes in the entire project resource sharing.
A static class frees resources only after the program has all finished.
Console is also a static class
Static classes are not allowed to be instantiated
Q: Why is it often used to write static classes?
A: Because you do not need to create an object, you only need the class name. Method Name. Convenient
Static classes need to store data, which accounts for memory.
Memory storage areas are divided into 3 major categories
Stack static storage area
From QQ program entry, QQ login once, use microblogging, games, music, space do not need to land
Multiple modules of the application. The user name and password exist in the static class.
When do static classes release resources? Release resources at the end of the program. Static classes consume resources at execution time, so do not use too many
Frees resources. GC Garbage Collection Garbage collector
6. Constructor function
Role: Helps us initialize the object (assigning values to each property of the object in turn)
A constructor is a special method:
1), the constructor does not return a value, and even void can not be written.
2), the name of the constructor must be the same as the class name.
Overload the same name as a different parameter
public student ();
Constructors are executed when an object is created
Constructors can have overloads.
***
There is a default parameterless constructor in the class, and when you write a new constructor, whether it is a parameter or
Without parameters, the default parameterless constructor is eliminated.
7. New keyword
Person Zsperson=new person ();
New has helped us do 3 things:
1), open a space in memory
2), create objects in the open space
3), invoking the object's constructor to initialize the object
8. This keyword
1), object representing the current class
2), the constructor for calling this class, shown in the class: this ()
Public Student (String name,Char Gender,int age,int Chinese,int math,Int中文版) {This. Name =NameThis. Gender = gender; this. Age = Age; This. Chinese = Chinese; This. Math = Math; This. 中文版 = 中文版; public Student (string name, char gender,int age): this (Name,gender,age,0,0,0< Span style= "color: #000000") does not replace the {this. name = name; this. Gender = Gender; this. Age = Age;}
No need to write redundant code
9. destructor
The destructor executes when the program finishes
Help US Release resources
. NET references to GC garbage collection are generally not called automatically by manual calls.
It's possible that the GC didn't release the resources right away at the end of the program.
If the resource is not released immediately, use the destructor (if you want to release the resource immediately)
Put the class inside
~student ()
{
Console.WriteLine ("I am a destructor");
}
10. Summary
Object-oriented: concept
Class syntax
Members: Fields, properties, methods, constructors
Object
New
This
The difference between static and non-static: the difference on the call
C # Basic notes (Nineth Day)