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.
The class is a building drawing object that is built up.
2. Class
Grammar:
[Public] class name
{
Field
Properties
Method
}
After writing a class, we need to create the object of this class,
So, the process of creating an object for 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.
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 ().
Both get () and set () are the readable writable properties.
Only get () does not have set () What we call a read-only property
No get () only set () What we call a write-only property
Field fields
Method methods
Property Properties
4. Access modifiers
Public: Publicly available, accessible anywhere.
Private: It can only be accessed within the current class, and this class is inaccessible.
5.
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.
6. Static and non-static differences
1), in a non-static class, there can be either an instance member or a static member.
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;
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.
Use:
1), if you want your class to be used as a "tool class", consider writing the class as static.
2), static classes in the entire project resource sharing.
A static class frees resources only after the program has all finished.
Stack static storage area
Frees resources. GC Garbage Collection Garbage collector
7. 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.
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.
8. 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
9. This keyword
1), object representing the current class
2), the constructor for calling this class, shown in the class: this
C # Object-oriented beginner (refer to Wisdom Podcast video)