C # Object-oriented Summary 1

Source: Internet
Author: User

1. Process-oriented-----> Object oriented

Process oriented: The process of accomplishing this, emphasizing the action of accomplishing it.

Object oriented: Find an object to help you do things.

Intended to write a generic code that masks differences.

We describe an object in code, typically by describing the properties and methods of the object.

We further encapsulate objects with the same properties and methods, abstracting out the concept of class.

Class is equivalent to a stencil in a factory, which determines the properties and methods that an object should have.

Objects are created from classes.

2. Class

Syntax: [Public] class name

{

Field

Properties

Method

}

After we have written 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 referred to as read-write properties.

Only get () has no set () We call the read-only property.

No get () only set () we call the 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.

Frees resources. GC Garbage Collection Garbage collector

7. Constructor function

Role:

Help 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.

Constructor constructors can be overloaded when an object is created.

There is a default parameterless constructor in the class, and when you write a new constructor, the default parameterless constructor is eliminated, whether it is a parameter or no argument.

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,

10, analysis enough function (*) ~

Destructors cannot be defined in structs.

Destructors can only be used on classes.

A class can have only one destructor.

Destructors cannot be inherited or overloaded.

Destructors cannot be called. They are automatically called.

Destructors have neither modifiers nor parameters

~Student ()        {            Console.WriteLine (" I am a destructor ");        }     //         destructors are executed when the program is finished // help us free resources         //GC Garbage Collection

C # Object-oriented Summary 1

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.