C # object-oriented basics,

Source: Internet
Author: User

C # object-oriented basics,

Object-Oriented Programming (OOP)

 

 

Class and Object

A class is the definition of an object, and an object is the implementation of a class.

[Attribute set information] [class modifier] class name [: class base] // defines the syntax format of the class. Square brackets can be omitted. The class modifier is public by default, the default implicit base class is object.

{Class member}

 

Attribute Set information: the c # language provides programmers with instructions for various entities in the program.

Partial classification modifier: private: only members of this class can access

Protected: can only be accessed by this class and its subclass (derived class)

Internal: accessible in this Assembly.

Public: completely accessible.

Abstract: abstract classes that contain abstract members cannot be instantiated and can only be used as base classes.

Sealed: sealed class. No other classes can be generated.

Class base: defines the direct base class of the class and the interface implemented by the class.

Class Member: it consists of two parts: one is a class member introduced in the form of a class member declaration, and the other is a member directly inherited from its base class.

Class member declarations mainly include constant declarations, field declarations, method declarations, attribute declarations, event declarations, constructor declarations, and destructor declarations.

When a field, method, attribute, event, operator, or construction Declaration contains a static modifier, it indicates that it is a static member and cannot be instantiated.

Access modifier for partially classified members: private/protected/internal/public, same as above

 

Constant Declaration: the value of a constant expression should be a value that can be calculated during compilation. static modifiers are not allowed, but like static members, they can only be accessed through classes.

[Attribute set information] [constant modifier] const Class Identifier = constant expression [] // square brackets can be omitted. Class modifier is public by default, and implicit base class is object by default.

 

Field Declaration: the variable declaration list can be separated by commas (,), and the variable identifier can also be used to set the initial value.

Readonly modifier, indicating read-only fields. A read-only field can be assigned a value during definition or constructor. Its value cannot be determined during compilation, but can only be determined during runtime. Constants can only be assigned values during definition. The constant value has been determined during compilation and cannot be determined in the program.

[Attribute set information] [constant modifier] class variable declaration list // square brackets can be omitted. The default modifier of a field is private,

 

Constructor and destructor: they are two special types in the class, which are used to initialize and recycle object Resources respectively.

If a class contains a constructor, it is called when the class object is instantiated. If the class contains a constructor, it is called when the object is destroyed.

The constructor name is the same as the class name. The name of the constructor is the same as that of the constructor. A Tilde is added before the constructor name.

Constructor: no return value. The method name must be a class name. If no constructor is provided, the system defaults to a constructor without parameters. You can define multiple Constructor (overload: different parameter types, the number is different and the order is different), static attributes and static Constructor (the execution of static constructor is controlled by the System)

Public Class Name (){}

 

Attribute: The main purpose of an attribute is to restrict the access permission of an external class to members of a class, which is defined at the class level.

[Attribute set information] [constant modifier] class property name

{

Get {}

Set {}

}

 

 

Object-oriented features: encapsulation, inheritance, and polymorphism.


Encapsulation is specific to classes. Classes encapsulate things with the same features,
It stores the attributes and methods of a class of things. objects are the specific implementation of classes,
Indicates a specific thing;
Properties and methods; however, the property values of each object and the results of method execution are different (except for static attributes and static methods );

The default modifier of the method is internal; the default modifier of the top-level class is internal (only public or internal );

 

One chestnut:

Object: Table

Attribute: Table Style, table height, table material

Method: Release books. When the fire is burned, the size can be scaled.

Encapsulation: defines the table class. The table class has the material attribute, which encapsulates (logical processing) and only keeps interface access.

What should be exposed and what should not be exposed? In the design of the class, the private attribute user cannot directly access the table. If the interface is provided to access the material attribute, you can modify the value of this attribute.

The difference between modifying attributes by method and directly modifying attributes is that modifying attributes by method can impose restrictions. If a bad merchant uses junk wood as a table. This method can be used as a limit.

Inheritance: an object (table) has a method (scaling), and another object (table) does not exist. The simplest thing is to inherit this scaling method.

Polymorphism: an object (table), each table has the same method (put something), but some put the computer, some put the book. This is polymorphism

 

But now I still don't know much about it. It's too difficult to understand and I have to think about it again.

Fill in so many traps first, and then fill in the remaining part.

 

Reprinted please contact

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.