C # object-oriented

Source: Internet
Author: User
Tags first string string methods

C # object-oriented is relative to process-oriented. process-oriented emphasizes functional behaviors, and tasks are completed by multiple methods. Object-oriented encapsulates functions, the completion task is completed by the object. object-oriented is not a substitute for process-oriented, but a transformation of ideas. in the past, I used to do things by myself. Everything was fine. Now I have made my achievements as a boss and handed it over to me.
1. What is a class,
It is a general term for a type of things. A template is fictitious, abstract, and does not exist.
2. What is an object?
Is a real individual, is the instantiation of the class.
3. How to instantiate an object
An object generated through a class is called declaring an object and instantiating an object. Syntax: Class Name Instance name (variable name) = New Class Name (); for example, person PS = new person (); to complete the declaration of an object; note = the previous step is to declare an object name (variable name), followed by opening up a piece of memory space and actually instantiating it, which means that PS actually exists in the computer. Of course, it must also be after instantiation. The object name. attribute name; object name. Method Name () calls the attributes and methods of the class. Note: The class does not occupy the system memory, and the object occupies the memory.
4. Static class access
Class with static modification, which does not need to be instantiated. attribute name and class name. the method name can be used to define the attributes and methods of the category. static class, which cannot be regarded as a new class as a static class. It is often used to implement function libraries.
5. Access Modifier
Attribute and Method: Public indicates public and can be accessed anywhere. Internal indicates that the object can only be accessed in this project. Protected indicates that the object is protected, it can only be accessed in this class and subclass; private refers to private and can only be accessed in this class. The default value is the private modifier.
Class: Public indicates public, which can be accessed anywhere. Protected is protected and can only be accessed in this class and subclass. Internal indicates that it can only be accessed in the project. The default value is internal.
6. Attributes
If a variable is defined in the method and the variable has a get or set method, the variable has a corresponding attribute. Why is it corresponding? Because variables are often modified by the public to protect a private variable modified by the private. In this way, the private variable can only be accessed through attributes to protect it. For example: Private int age; Public int age {get {return age}; set {age = value ;}}. This age is an attribute that allows external users to access the age, but cannot access the age. This protects the age field. You can set the attribute by right-clicking the field, and then selecting "refactor" to encapsulate the field. CTRL + R, e shortcut function. attributes are divided into read/write (including get and set methods), read-only (including only get methods), and write-only (including only set methods). You can set the relevant attributes as needed.
7. Automatic attributes
An attribute is essentially a method. It consists of get and set methods. the so-called automatic attribute means that only the attribute has no field, then the system will automatically generate a private field for us that only the attribute can be accessed. note that the get and set methods can only be used in pairs. If only get is used, no value can be assigned. In the same way, only set is used, or set is meaningless. in fact, the system reports an error.
8. Constructor
Syntax: [access modifier public] Name (with the same name as the class, no return value ){}.
Purpose: Create an Object Instantiation object and assign an initial value to the class members.
Benefits: <1. you can assign values to multiple attributes or fields. You do not need to repeatedly write instance names. <2. ensure that the user must assign an initial value to the specified member when creating the object <3. you can assign values to read-only attributes.
Parameters: automatically generated by the system. The default constructor has no parameters. When you customize the constructor, the system's default constructor will automatically disappear. to avoid errors, You can manually add a method without parameters. the constructor can be overloaded, that is, there can be constructor methods with multiple parameters.
9. inherit (:)
If no parent class is specified during class definition, the parent class is the object class. The object class is a direct or indirect parent class of any class. inheritance is a single inheritance and can only inherit from one class.
Subclass inherits the attributes and methods of the parent class, and can also have its own specific attributes and methods. when a subclass member is called, the program first calls the constructor of the parent class to initialize the member, and then executes the instantiation method of the subclass.
10. Indexer
How to define the indexer:
String This [int Index] {get {return "" ;}set {"" = value ;}}, string is the type of the indexer; [] is the parameter list, use the set method to obtain the User-Defined value through value; Use the get method to read the value. there can be more than one index parameter, and the type is not limited to int; almost any type.
11. Variable Params Parameters
Add the Params parameter before the method type to become a variable parameter. After one or several values of the same type are passed during the call, the method will automatically put the values in the array of the same type.
For example, test (Params int [] arry), but if the method has multiple parameters of the same type, the parameters modified by Params must be placed at the end. (For example, test (INT age, Params int [] arry ))
12. namespace
The namespace is used to solve the cognominal problem of classes. It can be seen as a "folder" (meaning of understanding) of classes. If the classes used are in the same namespce namespace, they do not need to be introduced. if not in the same namespace, You need to introduce. there are two methods:
<1. Write full name (class name. method name)
<2. Introduce the namespace using before calling it.
13. destructor
The Destructor is used to manually clean up memory resources, but the Garbage Collector in C # cleans up memory resources from time to time.
Note: The Destructor can only be defined in the class and cannot be defined in the structure. A class can have only one destructor and does not allow overloading. It cannot inherit or overload the destructor. It cannot call the destructor. It is automatically called, the Destructor has no modifiers or parameters. For example ~ Class Name {}, which is a destructor.
14. String
1. The string can be viewed as a char read-only array, and the char array can be traversed to output the string content. However, the string content cannot be modified directly by subscript, so it is read-only.
2. A character string is non-mutable. Once declared, the character string cannot be changed. Therefore, it can only be read. It cannot be directly modified through the index.
3. Indirect modification:
Copy the tochararray () method to obtain the new char array. After modification, call the new string (char []) constructor to create a New String object.
4. string is a string in C #, while string is a string in. Net framwork, which is actually the same.
5. common string Methods
<1. Equals () compare whether two strings are the same (= ).
<2. split () string segmentation, according to requirements to split the string; pay attention to the third overload method of Split (); the second parameter string. splitstringoptions. rempveemptyentries deletes spaces in the split array.
<3. Download the substring.
<4. indexof () returns the index of the specified character.
<5. tolower () and toupper () Get the string in lowercase or upper case. Note that the method does not change the original value of the string, but only generates a new string by using the method.
<6. Replace () replaces the specified character
<7. compare () compares two strings in ASCII order, returns 0 equal, returns-1 The first string is smaller than the second string, returns 1 the first string is greater than the second string
<8. Contains (): determines whether a string exists. A value of true or false is added.

Related Article

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.