C # class and Object

Source: Internet
Author: User
Component Programming does not abandon traditional object-oriented programming. On the contrary, component programming is the deepening and development of object-oriented programming. As the soul of object-oriented language, class has been widely used in C # language. Many very "sharp" component features are even directly packaged by class. Deep understanding of classes is naturally an important part of our "sharp XP.

Class

C # is a data structure that encapsulates data members, function members, and nested types. The data member can be a constant or a field. Function members can be methods, attributes, indexers, events, operators, instance builders, static constructors, and destructor. Except for some imported external methods, the declarations and implementations of classes and their members in C # are usually put together.
C # use a variety of modifiers to express the different properties of the class. Class C # of its protection level has five different restrictions and modifiers:

Public can be accessed at will;
Protected can only be accessed by this class and its inherited sub-classes;
Internal can only be accessed by all classes in the Assembly. The combination is the logical unit and physical unit after the classes in C # language are combined, the compiled file extension is often ". DLL or. EXE ".
Protected internal is a unique composite modifier that can only be accessed by all classes in the current composite and the inherited sub-classes of these classes.
Private can only be accessed by this class.
If it is not a nested class, only the public and internal classes in the namespace or compiling unit are modified.

The new modifier can only be used for nested classes, indicating hiding the type that inherits the same name of the parent class.

Abstract is used to modify abstract classes, indicating that the class can only be used as the parent class for inheritance, but cannot be instantiated. Abstract classes can contain abstract members, but they are not required. Abstract cannot be used together with new. The following is the pseudo code used in the abstract class:

Abstract Class
{
Public abstract void F ();
}
Abstract class B:
{
Public void g (){}
}
Class C: B
{
Public override void F ()
{
// Implementation of method F
}
}

Abstract class A contains an abstract method F (), which cannot be instantiated. Class B inherits from Class A, which contains an instance method g (), but does not implement the abstract method F (), so it must still be declared as an abstract class. Class C inherits from Class B and implements class abstraction method F (). Therefore, objects can be instantiated.

Sealed is used to modify a class as a sealed class to prevent the class from being inherited. At the same time, abstract and sealed modifications to a class are meaningless and prohibited.

Object and this keyword

The distinction between classes and objects is crucial for us to grasp OO programming. We say that a class is an encapsulation of its members, but the encapsulation design of the class is only the first step in our programming, and objects of the class are instantiated, implementing operations on its data members is the foundation for us to complete practical tasks. The instantiated object adopts the myclass myobject = new myclass () syntax. The new semantics here calls the corresponding builder. C # all objects will be created on the managed stack. The instantiated type is called an object. Its core feature is that it has a copy of its own unique data member. The data members held by these special objects are called instance members. On the contrary, data members that are not held by special objects are called static members and declared with static modifiers in the class. Static function members that only perform operations on static data members. In C #, static data members and function members can only be obtained through class name reference. See the following code:

Using system;
Class
{
Public int count;
Public void F ()
{
Console. writeline (this. Count );
}

Public static string name;
Public static void g ()
{
Console. writeline (name );
}
}
Class Test
{
Public static void main ()
{
A a1 = new ();
A a2 = new ();
A1.f ();
A1.count = 1;
A2.f ();
A2.count = 2;

A. Name = "CCW ";
A. G ();
}
}

We declare two A objects A1 and A2. For instance members count and F (), we can only reference them through A1 and A2. For static member names and g (), we can only reference them through type A, but not a1.name or a1.g ().

In the above program, we can see that we use this to reference the variable count in the instance method F. What does this mean? This keyword references the members of the current object instance. In the instance method body, we can also omit this and directly reference count. In fact, they have the same semantics. Of course, the static member function does not have the this pointer. This keyword is generally used to access members from constructors, instance methods, and instance accessors.

In the constructor, this is used to restrict hidden members with the same name. For example:

Class employee
{
Public Employee (string name, string alias)
{
This. Name = Name;
This. Alias = alias;
}
}

This is also used to express an object as a parameter when it is passed to other methods. For example:

Calctax (this );

This is even more indispensable when the indexer is declared, for example:

Public int this [int Param]
{
Get
{
Return array [Param];
}
Set
{
Array [Param] =Value;
}
}

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.