. NET basics step by step: [object-oriented objects and classes],. net Object-Oriented
Objects and Classes
This article briefly introduces the object-oriented knowledge point:
What is an object,Buddha said: A flower a world, a wood a float, a grass a heaven, a leaf a like, a sand a joy, a clean soil, a smile a dust, a read a quiet. Visible"All objects".
Object: the entity that contains data and operations.
Process-oriented: it focuses on the process of completing this task and the action of completing it.
Example: put an elephant in the refrigerator
1, Open the refrigerator door
2Put elephants in
3Close the refrigerator door
If we use an object-oriented approach to solve this problem, when people execute it differently, we need to tailored solutions for different people.
Object-oriented: Find an object to help you do things.
Put the elephant in the refrigerator
We take the refrigerator as an object:
1And the refrigerator door can be opened.
2Elephants can be pushed into the refrigerator.
3The refrigerator door can be closed.
This is what object-oriented means.
We describe an object in the Code by describing its attributes and methods.
Objects must be visible and tangible.
For example:
Lamp: Properties and Methods
Attribute:
Shape: Long
Brightness: 500 W
Color: white
Brand: XX
Method: Luminous
We further encapsulate these objects with the same attributes and methods and abstract the concept of classes.
Class is a model that determines the attributes and methods that the object should have.
The object is created based on the class.
Class is the drawing object of a building.
Class: encapsulated by attributes and Behaviors
Declaration class Syntax: [access modifier] class <class Name (custom)> {}
[Access modifier] class name
{
Field;
Attribute;
Method;
}
Code display:
Public class Student {// FIELD private string _ name; // attribute public string Name {// when you output an attribute, the get method get {return _ name;} is executed ;} // when you assign a value to the attribute, the set method set {_ name = value ;}} public void Say () {Console will be executed first. writeLine ();}}
View Code
The difference between a class and an object: A class is essentially different from an object. A class is an abstract description of a specific object, and an object is a real entity. objects are classified into classes, it is a process of summarizing the commonalities of objects. On the basis of the class, the process of materialized states and behaviors into objects is called Instantiation.
Here is a small case (console program ):
Static void Main (string [] args) {// Person sunQuan; // custom class // create Person Class Object Person suQuan = new Person (); suQuan. name = "Smile"; suQuan. age =-18; suQuan. gender = 'chun'; suQuan. CHLSS (); Console. readKey ();} // Person custom class public class Person {private string _ name; public string Name {// when the value of the output property is worthwhile, The get method get {return _ name;} will be executed ;} // when you assign a value to an attribute, the set method set {_ name = value ;}} private int _ age; public I will be executed first. Nt Age {get {return _ age;} set {if (value <0 | value> 100) {value = 0 ;}_ age = value ;}} private char _ gender; public char Gender {get {if (_ gender! = 'Male' & _ gender! = 'Female ') {return _ gender = 'male';} return _ gender;} set {_ gender = value ;}} public void CHLSS () {Console. writeLine ("My name is {0}. I am {1} years old. I am {2} student. I can eat, eat, drink, and sleep ~~~ ", This. Name, this. Age, this. Gender );}}
View Code
Running result:
Summary: classes are the specific implementation of objects, and objects are the abstract descriptions of classes.
Finally, make a small advertisement: QQ group: . NET Step by stepGroup Number:590170361 (Add group remarks: what you see in the blog Park)