Classes and objects

Source: Internet
Author: User

Unlike a structured programming language like C, which uses C # programming, all of the program code is placed almost in the class

, there is no function separate from the class. Therefore, a class is a basic unit of object-oriented programming.

In most object-oriented languages, a class can contain two types of Members: field and Method (Me

Thod). The two concepts of field and method are the term of object-oriented theory, which is universal to various object-oriented languages.

In a variety of specific object-oriented languages (such as C #), it is easy to understand this:

A field is a variable, and a method is a function.

The fields of a class generally represent the data that is processed in the class, and the methods of the class mostly represent the process of processing the data or for

To implement a particular function, the code in the method often requires access to the data saved by the field.

In C #, define several variables, write a number of functions, assemble the code in the following format, and then start a

A meaningful name completes the definition of a class:

[Public|private] class name

{

[public|private] Data type variable name;

[Public|private] Data type function name (parameter list)

{

}

}

In the definition of the above class, the square brackets represent this part optional, while the vertical bar represents the multiple selection. The change declared as public

The amount and function can be directly accessed by the outside world, and with this corresponding, private variables and functions, then private members of the class, can only

Used by the class itself.

The basic members of the constituent classes are briefly described below.

Fields of Class 2.1

field represents the data in a class, and defining a variable outside of all the methods of the class defines a word

Paragraph You can add public, private, and protected to represent access to a field before the variable. The following code shows

The two public fields defined in class student, age and SN, can be read by objects created by class student

or sets the value of the two fields.

You can give an initial value when you define a field, as follows:

Method of Class 2.2

(1) Concept of function

In the process of program development, it is often found that many need to implement or invoke a common function (such as selecting a text

The implementation of these features requires writing several lines of code. If you repeatedly write these functions where you call this function

Code, will make the whole program of code duplication, will increase the development effort, increase the difficulty of code maintenance.

In order to solve the problem of code duplication, most programming languages will complete a common function of multiple statement groups

Together, a name is used to represent the whole of these statements, and such blocks of code are called " functions".

Once the concept of " function " is introduced , the function name can only be written in the program where this common function needs to be called .

Represents all the code contained in the function, so that it is no longer necessary to write these functions repeatedly in multiple places

Code.

The appearance of function indicates that software development has entered the era of structured programming. Calling and writing various functions is a program

One of the main tasks in structured programming.

The syntax format for a function in C # is as follows:

Return value type method name (parameter list)

{

Statement 1;

Statement 2;

...

return expression;

}

The following is an example of a typical C # function:

The function needs to return a value to the outside world, implemented by the return statement.

If a function has no return value or does not care about its return value, the return value is defined as void.

(2) Definition and use of methods

Functions placed in a class (typically attaching an access modifier such as public and private) are called " square methods".

(method)".

The most basic way to access a method is through the object created by the class. For example, the following code is defined in class mathopt

An ADD () method:

public class Mathopt

{

public int Add (int x, int y)

{

return x + y;

}

}

You can access this ADD () method by creating an object of class mathopt by using the New keyword:

Class Program

{

static void Main (string[] args)

{

To create an object of the Mathopt class

mathopt obj = new mathopt ();

//method of invoking class through object, result saved in local variable

int result = obj. ADD (100, 200);

......

}

}

(3) Method overloading

Method overloading is an important extension of an object-oriented language, such as C #, to a structured programming language, such as C., see

The following code:

Class Mathopt

{

Add integers

public int Add (int x, int y)

{

return x + y;

}

Add floating-point numbers

Public double Add (double x, double y)

{

return x + y;

}

}

}

The above two functions have the following uniqueness:

(1) The function name is the same, all is Add;

(2) The parameter type is different, one is int and the other is double.

These two functions of the same name make up the " overloaded (overload)" relationship with each other .

Calling code for overloaded functions:

Mathopt mathobj = null; //Define Mathopt object variables

Mathobj = new mathopt (); //Create Objects

int Iresult=mathobj. ADD (100, 200); //Call the integer addition method of the class

Double Fresult = mathobj. ADD (5.5, 9.2); Floating-point number addition method for call class

Console.WriteLine ("100+200=" + iresult); Output results

Console.WriteLine ("5.5+9.2=" + fresult); Output results

Note the two method invocation statements, which are marked in bold. When you pass a method argument to a floating-point number, the parameter type is called

The Add (double, double) method of double invokes the Add (Int,int) method with the parameter type int when the argument to the method is an integer.

Function overloading is an important extension of object-oriented language to the characteristic of structured language, which is widely used in object-oriented programming.

The two functions that make up an overloaded relationship must meet the following conditions:

(1) The function name is the same.

(2) The parameter type is different, or the number of parameters is different.

It is important to note that the difference in function return value types is not a criterion for function overloading.

For example, a function

public long Add (int x, int y) {...}

is not associated with a function

public int Add (int x, int y) {...}

constitutes an overloaded relationship, because both functions have the same argument type and number, and only the function return value type is different.

Also note that C # is a case-sensitive language, so if you have the following two functions in a class:

public long Add (int x, int y) {...}

public int Add (int x, int y) {...}

Can be compiled and the program can run, but this is not to say that the two functions constitute an overloaded relationship, in fact,

C # thinks it's two completely different functions and doesn't have a relationship with each other!

Classes and objects

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.