Classes and objects in C # some knowledge "1"

Source: Internet
Author: User

First we need to know the three main features of object-oriented language (object-oriented language): encapsulation (encapulation), Inheritance (inheritance), polymorphism (polymorphism).

Introduction: Common Object-oriented language has c++,java,c# and so on. First of all to define a class, so that we can initially master the class exactly how to write.

public class Students

{

int SID;

int age; If private or public is not labeled, the default is private

............

public void GetName () {

......

}

}

The instantiation of the class and the invocation are the same as C + +.

For the access modifiers in front of the class, there are roughly a few

    1. pulic: For a code that is placed in the same combination as other code and other combinations that reference it, it is possible to apply this type and invoke its members and member functions.
    2. Private: You can access its members and member functions in this class, but the code outside of the class is not allowed to be used.
    3. protected: Members and methods of this class can only be used by the class, or its derived classes, others cannot (from this point of view, between public and private).
    4. internal: Members of this class can be used by the same combination of code, but cannot be used by other combinations of code.
    5. protected internal: Members of this class can be used by the same combination of code, or by a derived class of this class in any other combination.

In the previous C + + learning, has been aware of some object-oriented knowledge, this time in the C # Learning, a lot of things can be skipped, but for some common mistakes or need attention, bad understanding of the place, here to share to you:

1. Problems with formal parameters and arguments: an example illustrates:

This is the output example, it can be seen that the actual parameter in the incoming function is not changed its true value, but the value of the argument is assigned to the formal parameter, and then the parameters of the corresponding operation, the value of the actual parameter has no effect. However, if an object is passed in, it can be understood that the object's pointer is passed in, at which point the operation inside the method affects the object's properties.

2. Application of this

1. Avoid confusion: For example, this small code:

public void SomeMethod (int parameter) {

This.parameter = parameter;

In this code, the property of the current object is parameter by//can be referenced in this way, otherwise it will be masked by the formal parameter parameter.

}

2. The current object can be used as a parameter, for example:

return this;

or B.somemethod (this);

3. Call the overloaded constructor, such as the following code, before a constructor executes:

By example, you can see that this can call a constructor with parameters to implement a parameterless constructor.

3. Static member (Statics members)

A) static members can be considered as part of a class. Because it can be called directly by the class name.

b) There is no this pointer in the static method

c) Static methods are not allowed to use a non-static member.

4. Static constructor function

A) When the class is instantiated, it is called first.

b) Modifiers such as public,private are not allowed before static constructors

c) This is a good method if the attribute can only be assigned once.

5, Static class:

A) cannot be instantiated

b) cannot derive

c) cannot have non-static members and methods

D) Then I really don't understand what it's trying to do ... You can give me a comment and teach me.

Classes and objects in C # some knowledge "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.