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

C#

PROGRAM CLASSES

bject-oriented programming languages use classes Othat act as containers for data elements in your

program. Classes let other elements in your program, such as methods, process that data and perform functions such as displaying a calculation result as output.

Object-oriented programming languages make use of classes, which are reference types that act as containers for data elements in your program. Classes include class member characteristics such as the method the member performs, the events the member performs, or the properties of the member.

A class usually includes definition of the object and the implementation of that object. However, a class can have no implementation information, and C# refers to the members of such a class as abstract members, and the class is called

an abstract class. You can use an abstract class when you want its instances to only have basic functionality that you can override or augment with information from other classes.

C# refers to the members of a class as an object. When your program invokes the object, the program refers to the class and receives the object properties as well as any implementation methods, such as whether the program uses the object as an event.

You can determine how your program accesses objects in your class. For example, one object within a class can only access another object within that class. However, any class can access objects in any other class.

PROGRAM CLASSES

Console

 

 

 

 

 

The New Project window

 

 

 

 

 

 

¤ Click New Project in the

NET

Start Page.

appears.

Studio

 

 

 

Click the Console

 

 

 

 

 

 

 

 

 

 

Application icon in the

 

 

 

 

 

Templates pane.

Type a name for the file.

ˇ Click OK.

PROGRAMMING C# BUILDING BLOCKS 4

When you add a class in the Add Class Wizard, one of the first things you must do is identify a namespace. C# automatically adds a new namespace when you create a new class, but if you add a class within a class, you can specify a new namespace.

TYPE THIS:

using System;

namespace NewSpace

{

class NewClass

{

public static void Main(string[] args)

{

Console.WriteLine("This class has the namespace NewSpace.");

}

}

RESULT:

This class has the namespace NewSpace.

The Class1.cs code appears in the parent window.

Note: You can make more room for your Start page by clicking and dragging the right edge of the Start page until you reach the maximum size for the Start page.

 

Á Delete the Class1 code

° Run the program by

 

from the program.

pressing the F5 key.

 

Type the MyClass code.

 

 

 

The output string appears on the screen.

· Save the program as the filename.

67

C#

ADD A CLASS

a new C# project, the MDE window creates a new class so you can save the class code immediately. If you

you can do so in one of two ways: in code or accessing the Add Class

Explorer or Class View windows.

lets you create a class using the Add Class Wizard also lets you determine

creating will inherit information from

another class. If you want your class to inherit data from another, you must determine whether your class inherits from a base class or another class.

A base class is a single class from which all other classes will inherit. For example, if you set up class B to inherit from the base class A, you can set up class C to inherit from class B, and that way, class C will inherit all of the properties from class B as well as the base class A.

ADD A CLASS

Console

Applicatio

Click Start Programs Microsoft Visual Studio .NET 7.0 Microsoft Visual Studio

.NET 7.0.

¤ Click New Project in the Start page.

The New Project window appears.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

68

PROGRAMMING C# BUILDING BLOCKS 4

If you have programmed in C++ or Java before, you should be aware of changes in C# so you are not surprised. Because C# is closely related to C++ and Java, here are examples of the differences between several class-related keywords.

REFER TO A BASE CLASS

Java

super

C++

__super

C#

base

DERIVE A CLASS FROM A BASE CLASS

Java

class A extends B

C++

class A public B

C#

class A B

SPECIFY THAT A CLASS CAN BE INHERITED

Java

abstract

C++

abstract

C#

abstract

SPECIFY THAT A CLASS CANNOT BE INHERITED

Java

final

C#

sealed

Class View - Ad...

AddClass

Black Text

Add Class...

Add

Á Click the Class View tab at the bottom of the Solution Explorer window.

Right-click AddClass in the Class View window.

° Click Add.

The C# Class Wizard

· Click Add Class.

appears.

 

CONTINUED

69

C#

ADD A CLASS

f you decide to add a class using the Solution Explorer, Ithe procedure is different than adding a class from the Class View window. The most obvious difference is that you do not use the Add Class Wizard. Instead, you tell C# that you want to create a new class object. After you create

the class object, the class appears with the class skeleton already written for you so you can edit the class.

You can add as many classes to your program as you want. The class structure contains the namespace information, sample XML commentary, the class constructor, and comments telling you to add the constructor logic in place

of the comments. The class structure appears no matter what project you have created — even an empty Web project. You can edit the class to your content in the MDE parent window.

You can change the properties of the added class by using the Properties window below the Solution Explorer window. If you want to change the name of your class you can do that in the Solution Explorer as well. When you finish editing your class, it remains as part of your project unless you click the Exclude From Project option when you right-click the class name.

ADD A CLASS (CONTINUED)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Click to select the

 

± Click to select a class

 

 

 

access level from the

modifier in the Class

 

-down list.

modifiers area.

¡ Type a class comment in the Comment field.

Click Finish.

PROGRAMMING C# BUILDING BLOCKS 4

You can specify that your class is a base class by adding the abstract keyword to it.

TYPE THIS:

using System;

// This is an abstract class as denoted by the abstract keyword.

abstract class AbClass

{

static void Main(static[] args)

{

int string a = “An abstract class.”;

Console.WriteLine(a);

}

}

RESULT:

An abstract class.

AddClass

The NewClass class appears in its own tab within the parent window.

£ Click AddClass in the Class View window.

¢ Click {} AddClass.

The NewClass entry appears in the tree.

Click NewClass.

The NewClass properties appear in the Properties window.

§ Save the program as the filename.

71

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