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

C#

DECLARE ABSTRACT PROPERTIES

bstract properties are properties that you can place in Aabstract classes. If you remember, an abstract class

acts as the base class for inheriting classes. The same is true for inheriting and base interfaces. So, abstract properties are base class properties so properties in inheriting classes or interfaces can access them.

Like a class, you have to declare an abstract property by using the abstract keyword. An abstract property automatically comes with the virtual modifier just as a class does. The virtual modifier tells your program to check for overriding properties in inheriting classes automatically.

An abstract property does not declare any accessors. Instead, the abstract property provides only basic

information about properties that properties in inheriting classes can use. When the inheriting property combines its accessors with the abstract property information, the program creates a complete property that you can then use for computing property values.

Because the abstract property does not include any specific information — it merely “sets the table” for an inheriting property to perform its duty — the only text in the body of an abstract property is a semicolon after the get and set accessors. If you try to enter any other information, the MDE window will report the error and your program will not compile.

DECLARE ABSTRACT PROPERTIES

Start page appears. The New Project window

appears.

New Project.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

ACCESSING PROPERTIES 9

As with an abstract class or interface, there are restrictions that you must be aware of.

One restriction is that you cannot add any modifiers to an abstract property because C# automatically designates the abstract property as virtual. That means that you cannot add any of the other modifiers including static, override, or new. That also includes the virtual modifier. As long as you enter abstract as the property modifier and that abstract property resides in an abstract class or interface, you will have no problems.

Another restriction is that you cannot use the base keyword for retrieving property information from an abstract property. For example, if you declare an abstract property with an integer value of A and try to enter return base.A for returning the value of A, you will not be able to do so. The MDE window will catch your mistake if you attempt this maneuver.

Task List - 4 Build Error tasks shown (filtered)

 

Á Type the abstract property

Run the program by

 

code.

pressing F5.

 

 

Because the abstract

 

 

property cannot take a body,

 

 

the MDE window registers the

 

 

error.

° Save the program as the filename.

193

C#

INCLUDE PROPERTIES ON INTERFACES

ou can declare properties not only on classes but also Yon interfaces. When you have classes that process an

interface, those classes take the properties from those interfaces for further processing. Properties on an interface have a slightly different form than properties on a class.

You declare a property on an interface by using the new keyword. You can preface the keyword with attributes that C# uses to denote certain conditions; for example, you can tell your program that the property is obsolete and is no longer used.

After the new keyword, you can add the access type, which is the same as it is on class properties, classes, methods, and

other C# components. After you add the access type, you can enter the name of your interface property.

The interface accessors appear next enclosed in curly braces ({}). Neither the set and get accessors contain any other information in their bodies except for a semicolon. Like an abstract property, the interface property acquires information from other component properties (such as the one in a class) instead of processing properties itself. The get and set accessors serve only to determine whether the property is read-only (get), write-only (set), or readwrite (get and set).

INCLUDE PROPERTIES ON INTERFACES

Project window

Console in the

.

for the file.

Class View - In...

Point

Á Type the Point interface code.

Click the Class View tab.

° Click the plus sign next to the Method name.

· Click the plus sign next to the {} Method name.

Right-click Interface1.

Click Add.

± Click Add Property.

ACCESSING PROPERTIES 9

When a class or struct implements an interface, you can write your class or struct property to declare explicit interface member implementations. This is useful if you need to have an internal interface for performing tasks without passing it on to other parts of your program (and perhaps as output).

TYPE THIS:

interface Clones

{

object Clone();

}

interface Compare

{

int CompareTo(object template);

}

class Assembly: Clones, Compare

{

int Compare.CompareTo(object template);

}

RESULT:

The above example declares Compare. CompareTo as an explicit interface member implementation.

The C# Interface Property Wizard window appears.

¡ Click to select the property type from the drop-down list.

Type the property name in the property name field.

Leave the accessor get/set radio button selected.

£ Type a property comment in the Comment field.

¢ Click Finish.

The interface property

Save the program as the

code appears in the parent

filename.

window.

 

195

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