Objects and classes
This article formally enters the object-oriented knowledge Point summary:
What is an object, Buddha Yue: A Flower A world, a wood a life, a grass a heaven, a leaf as come, a sand a bliss, one side a pure land, a smile a Chenyuan, a read a quiet. " all things are objects "is visible.
Object: An entity that contains data and operations.
Process oriented: It is the process of accomplishing this thing, emphasizing the action of accomplishing it.
Example: Put an elephant in the freezer
1 , open the refrigerator door
2 and stuffed the elephants in
3 , close the refrigerator door
If we use object-oriented thinking to solve this problem, when the people who do it are different, we need to tailor the solution to each individual.
Object oriented: Find an object to help you do something.
Put the elephant in the freezer
We use refrigerators as objects:
1 , the refrigerator door can be opened
2 , elephants can be stuffed into refrigerators.
3 , the refrigerator door can be closed
This is what object-oriented is going to do.
We describe an object in code by describing the properties and methods of the object
The object must be visible and palpable.
For example:
Lights: Properties and methods
Property:
Shape: Long
Brightness: 500W
Color: White
Brand: XX
Method: Glow
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.
Classes: Properties and behaviors encapsulate the
Syntax for declaring a class: [Access Modifier]class< class name (custom) >{}
[ access modifier] class name
{
field;
attributes;
method;
}
Code Show:
Public classStudent {//Field Private string_name; //Properties Public stringName {//The Get method is executed when you export the property's worth Get{return_name;} //The Set method is executed first when you assign a value to a property Set{_name =value;} } Public voidSay () {Console.WriteLine (); }}
View Code
The difference between a class and an object: Classes and objects are essentially different, classes are abstract descriptions of concrete objects, objects are real entities, and objects are summed up as classes, which is the process of generalizing the generality of objects; On the basis of classes, the process of instantiating state and behavior into objects is called instantiation.
Here is a small case (console program):
Static voidMain (string[] args) { //Person Sunquan;//Custom Classes//to create an object of the person classPerson Suquan =NewPerson (); Suquan.name="smiles"; Suquan.age= - -; Suquan.gender='Spring'; SUQUAN.CHLSS (); Console.readkey (); }/ /Person Custom Class Public classPerson {Private string_name; Public stringName {//The Get method is executed when you export the property's worth Get{return_name;} //The Set method is executed first when you assign a value to a property Set{_name =value;} } Private int_age; Public intAge {Get{return_age;} Set { if(Value <0|| Value > -) {Value=0; } _age=value; } } Private Char_gender; Public CharGender {Get { if(_gender! ='male'&& _gender! ='female') { return_gender ='male'; } return_gender; } Set{_gender =value;} } Public voidChlss () {Console.WriteLine ("My name is {0}, I am {1} years old this year, I am {2} born, I can eat and drink to sleep yo ~ ~ ~ ~", This. Name, This. Age, This. Gender); }}
View Code
Operation Result:
Finally, the class is the concrete implementation of the object, the object is the abstract description of the class
finally make a small ad: QQ Group: . NET step by Step screen number:590170361 (Dabigatran Note: Blog Park to see)
. NET Foundation Step by step [object-oriented objects and classes]