[. NET object-oriented programming fundamentals] (9) Members of a Class (fields, properties, methods)

Source: Internet
Author: User

[. NET Object-oriented Programming fundamentals ] (9) members of the class (fields, properties, methods)

The class of the person previously defined includes the following members: Fields, properties, methods, events , and so on, in addition, the nested class that is mentioned earlier is also a member of the class.

A. The members of a class are divided: static and non-static members

B. Static member with static identity, not identified by default is non-static member

C. Static members are class-owned, and dynamic members are all instances of the instance, that is, the object

D. Static members are shared for all instances of the class, regardless of the number of instances or replicas of the class, and static members occupy only one area. A non-static member creates a memory field in each instance of the class.

Here are the main members of the class: Fields, Properties, methods

1. Member of Class-field

Field declaration: (static/readonly) <Type> < variable name >

A. A private variable that can be understood as a class, usually private.

B. The definition of a field usually begins with a lowercase letter or "_".

C. The field declaration modifier has both static and ReadOnly (read-only).

The D field is usually private, so you generally do not need to use an access modifier.

Example:

1    Static int eyescount=2; 2    ReadOnly int earscount=2;  

2. Member of Class-property

A. A public variable that can be understood as a class, usually public

B. Properties have get and set two methods.

The c.get accessor returns the same data as the property declaration type, meaning that the value or reference of the internal field can be obtained when the call is made.

The d.set accessor does not display the set parameter, it has an implicit parameter value, which is called when it is invoked, and can be assigned to a property's inner field or reference.

E. Because the members of a class are private by default, because the property is public, the modifier public is used to declare a property to be publicly available during object-oriented development.

F. properties can ignore get or set accessors, but not both.

Example:

1 string_country;2 //read/write Properties3   Public  stringCountry4 {5     Set{_country =value;}6     Get{return_country;}7 }8 //read-only properties9  Public  stringCountryonlereadTen { One     Get{return_country;} A }         - //Write-only property -  Public  stringCountryonlywrite the { -     Set{_country =value;} -}

6. Members of the class-Methods (method)

declaration: (Access modifier) < type > < method name >{method body}

Call the:[< class name. >]| [< Instance Object name .>]< method name > ([< argument list;])

Definition: A member of a class used to perform calculations or other behavior

static method:
Methods are divided into instance methods and static methods (with the members of the previously spoken class)

Only static fields can be called in static methods and non-static fields are not allowed

Method Parameters:

value parameter : does not contain any modifiers. The formal parameter in the method is a copy of the argument, and the parameter changes do not affect the value of the argument in memory, and the argument is safe.

reference parameter : declared with the ref modifier.

Refkeyword causes parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameter in the method are reflected in the variable.
To userefparameter, both the method definition and the calling method must be explicitly usedrefkey word.
Passed torefthe parameters of the parameter must be initialized first. This with outdifferent, outparameters do not need to be explicitly initialized before they are passed.
property is not a variable and therefore cannot be used as arefparameter passing.
Althoughrefand the outthey are handled differently at run time, but they are handled the same way at compile time. Therefore, if a method usesrefparameter, and the other method uses the outparameter, you cannot overload both methods. For example, from a compilation point of view, the two methods in the following code are identical. Attempting to do so will result in the code not being compiled.
If a method usesrefor outparameter, and the other method does not take these two types of parameters, it can be overloaded.

code example:

//calledDouble[] numbers =New Double[] {1,2,3,5.5 };Doublei =0; Myaddoperation (Numbers,refi); Console.WriteLine ("twice times the result of the calculation is: {0}", i*2); Console.ReadLine ();//reference parameter method declaration Public Static voidMyaddoperation (Double[] numbers,ref Doubleresult) {Result=0; foreach(DoubleNuminchnumbers) Result+=num; Console.WriteLine ("The result of the calculation is: {0}", result);}

output Parameters : declared with the out modifier. similar to ref , it also operates directly on an argument. The Out keyword must be explicitly specified in both the method declaration and the method invocation . The Out parameter declaration does not require a variable to be initialized before it is passed to the method, because its meaning is only used for output purposes. However, before the method returns, you must Assign a value to the out parameter.
The out keyword causes parameters to be passed by reference. This is similar to the ref keyword.


differs from ref :
Ref requires that a variable be initialized before it is passed.
Although a variable passed as an out parameter does not need to be initialized before it is passed, the method needs to be called to assign a value before the method returns.

Example:

1 //called2 Double[] numbers =New Double[] {1,2,3,5.5 };3 Doublei =0;4Myaddoperation (Numbers, outi); 5Console.WriteLine ("twice times the result of the calculation is: {0}", i*2);6 console.readline ();7 8 //output parameter method declaration9  Public Static voidMyaddoperation (Double[] numbers, out Doubleresult)Ten { Oneresult =0; A     foreach(DoubleNuminchnumbers) -Result + =num; -Console.WriteLine ("The result of the calculation is: {0}", result); the}

array-type parameter : declared with the params modifier. the params keyword is used to declare a variable-length argument list. Only one params parameter can be included in a method declaration . the params parameter is useful in cases where the number of arguments is variable, as shown in the following example:

1 //Calling Methods2 Double[] numbers =New Double[] {1,2,3,5.5 };3Console.WriteLine ("The result of the calculation is: {0}", Myaddoperation (numbers));4 5 //array-type parameter declaration6  Public Static DoubleMyaddoperation (params Double[] numbers)7 {8     Doubleresult =0;9     foreach(DoubleNuminchnumbers)TenResult + =num; One     returnresult; A}

Virtual Method (virtual method)

The virtual keyword is used to decorate a method in a base class. There are two things you can do with virtual:

Scenario 1: The virtual method isdefined in the base class , but the virtual method is not overridden in the derived class. In a call to a derived class instance, the virtual method uses the method defined by the base class.

Scenario 2: The virtual method is defined in thebase class and then overridden in a derived class by using override . In a call to a derived class instance, the virtual method uses the derived overridden method.

Abstract Method (Abstraction method)

The abstract keyword can only be used to decorate a method in an abstraction class, and there is no specific implementation. An implementation of an abstract method must be implemented in a derived class by using the override keyword.

(for abstract classes, explained in more detail later)

Key points:

1. only static fields can be called in static methods and non-static fields are not allowed

2. method with no return value, type void

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

Back to Catalog

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

[. NET object-oriented programming fundamentals] (9) Members of a Class (fields, properties, 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.