c#-three main object-oriented features-encapsulation (encapsulating member variables, encapsulating member methods)

Source: Internet
Author: User
Tags modifiers

Packaging:

Encapsulation is the collection of data or functions in a unit (what we call a Class). Encapsulated objects are often referred to as abstract data types.

The meaning of encapsulation is to protect or prevent the code (data) from being accidentally destroyed by us.

Encapsulation can encapsulate member variables and encapsulate member methods

=====================================================================================================

Encapsulate member variable encapsulation steps:

1, new class--

An object-oriented unit is a class, a. cs file, just like a text document, where C # code is placed

Create a new class: In Solution Explorer, right-click the namespace name → add → new project or class → Name class

(User-written class-user custom type

If you want to use our custom type to produce an object must have an instantiation process: class name Definition name = new constructor (); such as Student st=new student ();)

For example, create a student class:

Class student is the class name (the class created by default access modifier is internal, hidden display)

2, write the constructor--

the basic structure of a class is a constructor -a function that creates a class (each class has a constructor, which is hidden by default)

How do I write out a hidden constructor?

The red border area in the figure is the constructor for the student class, which is public at this time

Each class has a constructor, the access modifier must be public, and no return value is required, and the method name is consistent with the class name

"What is public?" Public is an access modifier

Access modifiers are defined as access and access levels for functions, with access modifiers of four types: public, private (private), internal (internal), protected (protected)

Public-publicly available, accessible anywhere, but with reference to namespaces
Private-The inside of the class can only be accessed

Internal-Internal, default, assembly accessible, assembly is namespace
Protected-protected, class-Inside and class-parent classes and subclasses can access the "

3. Fill in the information in the class--

For example: Student information including study number, name, gender, age (_code, _name, _sex, _age called member variables )

The underscore is for encapsulation at a time when the access modifier is public and the main function can be called and the assignment modified.

4, the Encapsulation (fixed format)--

Encapsulation Method One: (1) in the new class, the access modifier of the information is modified to private, in the main function, the method of the class cannot be called

(2) Write the method call function in the new class, write a public, allow the main function to access the call, the type is the same, the name is removed underscore (encapsulation function called attribute )

Get is read ( attribute value ) set is Assignment ( property Assignment ) at this time the main function cannot call _code, but can call code, but assignment is to assign value to _code

Package method Two: Click on the method name, right mouse → refactoring → package fields → dialogs without modification, enter, enter

Encapsulation method Three: In method two, you can see the Package field after the shortcut key ctrl+r,e, and then press two times enter Skip dialog box to complete the encapsulation

5. Complete Package

At this point, there is a problem, gender in addition to input male or female, but input other information can still be assigned, how can only be assigned to male and female?

Set property when modifying _sex assignment

Because sex has only two options for men and women, you can return TRUE or flase with a bool type to show men and women, such as true is male, false is female

The input is a male or female, _sex received true or false, so the input text into the bool type, at this time _sex can not receive the exception of true and false unexpected data, so the error

Or with the <bool?> nullable bool type:

When normal use, input error returns true by default

At this point, only the set in the function has no get, at which point the function can only be assigned value, cannot be evaluated

So how do you value it? Add a new function Sex1,get returns the value return _sex, if True returns "male", If False returns "female", at which point the main function accesses the SEX1

=====================================================================================================

Encapsulate Member method Encapsulation steps:

Methods are divided into common methods and static methods.

=====================================================================================================

c#-three main object-oriented features-encapsulation (encapsulating member variables, encapsulating member methods)

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.