C # basic notes (ninth day ),

Source: Internet
Author: User

C # basic notes (ninth day ),

1. process-oriented -----> object-oriented

Process-oriented: it focuses on the process of completing this task and the action of completing it.

Put the elephant in the refrigerator
1. open the refrigerator door
2. Put the elephant in and kiss the elephant's ass.
3. Close the refrigerator door

If we use the process-oriented idea to solve this problem, when the people who execute it are different,
We need to customize 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:
1. The refrigerator door can be opened
2. Elephants can be pushed into the refrigerator.
3. The refrigerator door can be closed.

Object-oriented: it is intended to write a common code to avoid differences.


Try to describe the features and actions of James and James.
Name: James
Gender: male
Height: 180
Body weight: 70 kg
Age: 22 years old
Eat, drink, and sleep. Everything is normal.
Eat, drink, and gamble

Name: Li Si
Gender: male
Height: 180
Body weight: 70 KG
Age: 23 years old
Mentally Handicapped, all healthy


We describe an object in the Code by describing its attributes and methods.
The object must be visible and tangible.

Lamp: Properties and Methods
Attribute:
Shape: Long
Brightness: 500 W
Color: white
Brand: XX
Method: Luminous


Fan: Properties and Methods
Shape: three blades
Color: white
Brand: XX
Method: rotate, Fan


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 of a building, and the object is the built building.

2. Class
Syntax:
[Public] class Name
{
Field; (store data and store multiple values)
Attribute;
Method;
}
After writing a class, we need to create the class object,
Therefore, the object process for creating this class is called class instantiation.
Use the keyword new.

This: indicates the object of the current class.
Classes do not occupy memory, while objects occupy memory.

There were only classes and no objects before. When we instantiate an object and assign a value to it (object), the object will appear. Subsequent operations will be performed on this object. This is an object-oriented operation.

Custom classes, not provided by the system, are written by yourself.
By default, fields have initial values. The field occupies memory.

3. Attributes
Attribute is used to protect fields, assign values to fields, and set values.
The essence of an attribute is two methods: get () and set ().
Get () and set () are both readable and writable attributes.
Only get () does not have set (). We call it a read-only attribute.
Without get (), only set () is called write-only attribute.

Fields Field
Methods Method
Property

Set is to assign the passed value to the field through the attribute.

One is called when a value is assigned, and the other is called when a value is assigned.
Use the two methods in the attribute to limit the field.
When you assign a value to an attribute, the set method is executed first.
When you output the attribute value, the get method is executed. (When outputting the attribute value) when outputting this. Name in the Method

In the process, the attribute is not worth it, but it is just a transition and then processed.

* *** The field is a woman, and the attribute is a man.
When a woman is at home, all the things of dealing with the outside world are done by men.

4. Access Modifier
Public: public and accessible anywhere.
Private: private, which can only be accessed within the current class. If this class is exclusive, it cannot be accessed.

5,
After we create a class object, we need to assign values to each attribute of this object.
We call this process object initialization.

Judge the value in set and the value of the field in get.

Set assigns a value to the attribute value to the field, and get returns the value of the field (a limitation can be made before return)

If the output field in the method does not output the attribute, the get judgment will not be used, and there is no way to limit it.

6. Differences between static and non-static
1) in a non-static class, you can have both instance members and static members. (Non-static fields, attributes, methods and static fields, attributes, and methods)
2) When calling an instance member, you need to use the object name. instance Member;
When calling a static member, you must use the class name. static member name;
Get {return person. _ name ;}
Set {person. _ name = value ;}

Summary: static members must use the class name for calling, while instance members use the object name for calling.
In a static function, you can only access static members, but not instance members.
In Instance functions, you can use both static members and instance members.
Only static members are allowed in a static class, and instance members are not allowed.

Why does a static class not allow the creation of an object because it is called with the class name? It is useless to create an object. It cannot be used. It is not syntactically used.

Usage:
1) if you want your class to be used as a "tool class", you can consider writing the class as static.
Frequently used, such as console
2) Static classes share resources throughout the project.
The static class releases resources only after all programs are completed.

Console is also a static class
Static classes cannot be instantiated.

Q: Why do I need to write static classes frequently?
A: Because you do not need to create an object, you only need the class name and method name. Convenient

Static classes need to store data, occupying the memory.
Memory storage areas are divided into three categories
Static storage area of heap Stack

Log in from the QQ program, log on to QQ once, and log on to Weibo, games, music, and space.
Multiple application modules. The user name and password must be in the static class.

When Will static classes release resources? Release resources when the program ends. QQ has been withdrawn, but the QQ space is still open, so the static class should not be too much in a project, because it will always consume your resources during execution.

Release resources. GC Garbage Collection Garbage Collector


7. Constructor
Purpose: Help us initialize the object (assign values to each attribute of the object in turn)
Constructor is a special method:
1) The constructor does not return values and cannot write void.
2) The constructor name must be the same as the class name.

Reload parameters with the same name

Public student ();

The constructor is executed when an object is created.
Constructors can be overloaded.
***
Class has a default non-parameter constructor. After you write a new constructor
If there is no parameter, the default non-parameter constructor is killed.


8. new Keyword
Person zsPerson = new Person ();
New helps us do three things:
1) Open up a space in the memory
2) create an object in the opened space
3) Call the object's constructor to initialize the object

9. this keyword
1) represents the object of the current class
2) Call the constructor of the class displayed in the class: this ()

Public Student (string name, char gender, int age, int chinese, int math, int english) {this. name = name; this. gender = gender; this. age = age; this. chinese = chinese; this. math = math; this. english = english;} public Student (string name, char gender, int age): this (name, gender, age, 0) replace {this with 0 if no. name = name; this. gender = gender; this. age = age ;}

 

No redundant code writing

10. destructor
At the end of the program, the Destructor is executed.
Help us release resources
. Net references GC Garbage collection, which is automatically called unless manually called.
It is possible that at the end of the program, GC does not immediately release resources for us.
If the resource is not immediately released, use the Destructor (if you want to release the resource immediately)

Put in class
~ Student ()
{
Console. writeline ("I am an destructor ");
}

11. Conclusion
Object-oriented: Concept
Class syntax
Member: field, attribute, method, constructor
Object
New
This
Static and non-static differences: Differences in calls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.