s1/c# Language and database technology Fundamentals/03-using attribute upgrade MYbank

Source: Internet
Author: User
Tags modifiers

Access modifiers

In an application, access modifiers can be used to decorate class member fields and methods to qualify a class member's accessibility level. Declaring members of a class (variables or methods) as public means that these class members can be accessed by other classes. If you use private to restrict access to class members, these class members can only be used within that class, and other classes do not have access to them.

Declaring a field or method as public means that the other class can access the field or method, and if it is declared private, the field or method can only be used in this class and other classes cannot be accessed.

Access modifiers

Access rights

Access level

Public

Not subject to any restrictions

Highest

Private

Only the inside of the class can be used

Minimum

Attention:

If you do not specify an access modifier before the member definition, the access to this class member defaults to internal, and the meaning of internal is described in the following lesson, which we can now understand to be accessible only within this project.

In an object-oriented programming language, the This keyword refers to the current object itself. This allows you to refer to the member variables and methods of the current class. If the code in the Student class SetName () in the above question is "_name=_name;" rewritten as "this._name=_name;".

Encapsulate fields with attributes

1. Properties of C #

In C #, fields are usually private. If you want to access a field in a class, you need to access its implementation through get and set, which combines the way fields and methods are implemented, which we call attributes.

Grammar:

private string _name;

public string Name

{

Get{return _name;}

Set{_name = value;}

}

The get accessor is used to return the value of the corresponding private field. A get accessor is similar to a method, and you must return the value of a field with a return statement. Executing a get accessor is equivalent to reading the value of a private field in a class.

The set accessor is used to set the value of the corresponding private field. A set accessor is similar to a method that returns a type of void. He uses an implied input parameter of value. When you assign a value to a property, the set accessor is called and the new parameter is assigned to the corresponding field.

2. Data type of attribute

When you define a property in a class, the data type of the property must be the same as the type of field that he accesses. For example, if the age field _age is an integer, then its property, ages, must be an integer type. If the last Name field _name is a string type, then its corresponding property name must also be of type string.

Note: The type of the property can be a class or an array

3. Access types for attributes

In addition to constraining data access, you can set the read and write properties to qualify its access type. The access type of the property is divided into the following three types.

A read-only type that contains only the get accessor.

Write-only property that contains only the set accessor.

Read-write properties, including get and set accessors.

4, how to quickly create attributes in the code, right-click → refactoring → encapsulation fields.

Specification: In C #, a different naming method is used when naming private fields and properties of a class.

When naming a private field for a class, start with an underscore "_" followed by a camel-named letter with the first letter in lowercase. If the field consists of multiple words, the first letter of the word is capitalized, such as _cardid, _stuname.

When naming properties for a class, use the Pascal nomenclature, which is to capitalize the first letters of multiple words that make up the name of the attribute, such as Cardid, Stuname.

Tip: The shortcut key for the encapsulated field is ctrl+r,e.

What is the difference between a field and a property in C #?

You typically designate a field as private and use it inside a class. Specifies the property as public, exposes it externally, and provides secure, valid range of protection to the field through get or set access.

What is the difference between a property and a method in C #?

The property set accessor and get accessor in C # do not apply "()" since the accessor does not return a value, so you do not need to specify void.

5. Object initializer

In the main () method of Example 4, a student object is instantiated, and then the property is assigned a value, such as the following code:

Student student=new Student ();

student.age=-20;

In C #, an object initializer is provided that makes it easy to assign values to an object's properties. As in the following code:

Student student=new Student () {age=-20};

When you have more than one property in a class, you can use the object initializer to assign values to multiple properties at the same time. Use {} to enclose multiple attributes with a comma interval. For example, you can add a property to the _name field of the student class for example 4:

public string Name

{

get {return name; }

set {name = value; }

}

The object initializer allows you to assign values to the Name property and the Age property:

Student stu=new Student () {name= "Mike", age=20};

Packaging

In object-oriented programming, encapsulation can be understood as the technique of keeping data and manipulation methods together, or the process of selectively hiding or exposing properties and methods in a class. The specific implementation of encapsulation is to encapsulate member variables, methods, and properties in the form of classes, communicating with each other through message passing between classes or objects, and passing the message is done by invoking the method exposed by the class.

Benefits of Encapsulation:

1. Avoid the use of illegal data assignment.

2, ensure the integrity of the data.

3, to avoid the internal changes in the class, resulting in the entire program modification.

Tip: If we write a code to implement two number exchanges, we find that the following code should be put out in a separate method, what should we do?

int temp;

TEMP=NUM1;

num1=num2;

Num2=temp;

Solution:

Using Visual Studio, you can turn a piece of code directly into a method, the refactoring feature of Visual Studio. So how do you use Visual Studio to generate a method? The operation is simple:

Select the code that you want to separate in the method right-click and choose refactor → extract method from the shortcut menu that pops up.

When selected, the method naming dialog pops up and the method is named swap, and the method signature is previewed in the dialog box.

When you click the OK button, the generated code looks like this.

Private static void Swap (ref int NUM1,REF int num2)

{

int temp; Intermediate variables

Temp =NUM1;

num1=num2;

Num2=temp;

}

The swap () method obtained from the above operation is similar to our own defined method. But there are two different, respectively, as follows.

1. The declaration part of two parameters in the method is decorated with the ref keyword. About the REF keyword we will learn in the next section of this chapter.

2. Added a keyword static (static) before the method swap (). The use of the static keyword will be explained in detail later in the study.

Value passing and reference passing

A value type variable directly contains its data, which differs from a reference type variable, which contains a reference to its data. Therefore, passing a value type variable to a method means passing a copy of the variable to the method. The changes to the parameters that occur within the method have no effect on the raw data stored in the variable. If you want the method that you are calling to change the value of a parameter, you must pass it by reference using the ref or out keyword.

Note: Passing parameters by reference also uses a ref-decorated parameter when calling a method.

Comparison value passing and reference passing

In summary, a value pass is a copy of the value of a variable that is passed to the method so that the parameter of the method is the same as the value of the argument. Modifying a parameter in a called method is also an update to the data of the actual parameter replica, and does not actually change the value of the argument.

Reference passing is a reference to the object being passed to the parameter of the method, so that the called method changes directly to the reference object, affecting the original value of the argument.

s1/c# Language and database technology Fundamentals/03-using attribute upgrade MYbank

Related Article

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.