Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

INCLUDE NON-STATIC METHODS

The non-static status is the default for all methods. Non-static methods, also called instance methods, rely on an instance of the class — that is, the non-static

method relies on the information it receives from an object generated by the class. Once the non-static method receives that object it provides the object with its implementation instructions and sends the object back out into the class for further processing.

The non-static method is best if you know that the class will generate an object for the method. If you create a method in an inherited class, then the non-static method is the only

choice. A static method belongs to its class, but a non-static method can take objects from inheriting classes. You can also set non-static methods to override or be overridden by other non-static methods in other inherited classes in your class family or from the base class.

Your non-static method does not accept objects automatically. You must tell the method that you want to accept the value by using the this keyword. When you use the keyword this in your method, the referenced object receives a type that matches the object type and a value that acts as a reference to the object.

INCLUDE NON-STATIC METHODS

Visual C# Projects

Console

Applications

Start page appears.

The New Project

New Project.

window appears.

 

Click the Console

 

 

 

 

 

Application icon in the

 

Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING METHODS AND EVENTS 6

You can reference a non-static method with the member-access format so you can point directly to the member you want the method to call.

 

TYPE THIS:

 

 

RESULT:

 

 

 

 

 

 

 

 

 

 

using System;

 

 

First name? John

 

 

public class Name {

 

 

First name: John

 

 

public string first;

 

 

 

 

 

public string last;public Person () { }

 

 

 

 

 

public Person (string first, string last) {

 

 

 

 

 

this.first = first;

 

 

 

 

 

this.last = last;

 

 

 

 

 

}

 

 

 

 

 

class Main: Person {

 

 

 

 

 

public static void Main() {

 

 

 

 

 

Console.Write("First name? ");

 

 

 

 

 

String first = Console.ReadLine(); //accepts input

 

 

 

 

 

Person a = new Person (name, id);

 

 

 

 

 

Console.WriteLine("First name: {0}", a.first);

 

 

 

 

 

}

 

 

 

 

 

}

 

 

 

 

 

 

 

 

 

 

Class View - N...

Class1

void

int

Add Method

Add

Á Click the Class View tab.

 

· Right-click the

The C# Method

Note: The Method signature field at

 

 

 

Click the plus sign (

)

class name.

Wizard appears.

the bottom reflects the changes to

 

 

 

Click Add Add Method.

 

Type the method name in

the method code as you type

next to the Method name.

 

 

 

 

 

information into the wizard fields.

 

° Click the plus sign (

)

 

 

 

the Method name field.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CONTINUED

 

next to the {} Method name.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

127

C#

INCLUDE NON-STATIC METHODS

When the non-static method processes the instance of a class, C# creates a copy of all instance (that is, object) fields for the method to process. This

ensures that a copy of the instance remains in the class while your class is being instructed by the non-static method. Once the object leaves the non-static method, the method-trained copy replaces the original that was in the class.

The earlier discussion in this chapter about static methods included information about simple names and how the declaration of those names can affect processing in a static method. With non-static methods the same rules apply.

If you try to evaluate a static variable in a non-static method, you will receive an error and the MDE window will prohibit you from compiling your program.

If you have a non-static method that another method in another inheriting class can override, be sure that your overriding non-static method can process the variables in your class correctly. If you do not, you may encounter processing errors because the new, overriding method may not be able to process all the variables in your class. The same holds true if you override a non-static method in another inheriting class.

INCLUDE NON-STATIC METHODS (CONTINUED)

 

 

 

£ Type the code for the

 

The method code appears

 

in the parent window.

One method.

 

Type code that establishes

¢ Type code and an

 

 

the First class and move

override method in the

 

the method code within the

Second class.

 

First class.

 

PROGRAMMING METHODS AND EVENTS 6

You can represent a class object in the memberaccess format as well for precise representation of your object. Though the member-access E.M format is the same as with static methods, the E cannot represent a type. Instead, the E must represent the class instance. Usually the member-access format does not include the identifier signified by M because the instance expression signified by E is all the information needed.

Another reason for using the member-access format is that you can perform a member lookup. A member lookup evaluates simplename or member-access forms in an expression or statement.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rename the Class1 class

 

 

§ Type the Main method

 

 

 

as Output.

 

that outputs the values.

Press the F5 key.

Save the program as the

The Second class

filename.

 

overrides the First class

 

and produces two output

 

lines of Second.One.

 

129

Соседние файлы в папке c#