没有合适的资源?快使用搜索试试~ 我知道了~
首页C# 入门经典 2008 part2
Visual C#已成为在.NET平台上进行开发的首选语言。Visual C# 2008融C++的灵活性和强大功能与Visual C#的简单性于一身。《Visual C# 2008入门经典》通过开发一个图片查看程序,全面阐述了使用Visual C# 2008开发应用程序的基本知识。 《Visual C# 2008入门经典》分为五部分,共24章。第一部分介绍了Visual C# 2008速成版开发环境,引导读者熟练使用该IDE;第二部分探讨如何创建应用程序界面,包含窗体和各种控件的用法;第三部分介绍了编程技术,包括编写和调用方法、处理数值、字符串和日期、决策和循环结构、代码调试、类和对象的创建以及图形绘制等;第四部分阐述了文件和注册表的处理、数据库的使用和自动化其他应用程序等;第五部分介绍了应用程序部署并概述了.NET框架。 《Visual C# 2008入门经典》通过简洁的语言和详细的步骤,帮助读者迅速掌握使用Visual C# 2008开发应用程序所需要的基本知识。《Visual C# 2008入门经典》适合没有任何编程经验的读者和Visual C#新手阅读,也可供大中院校的学生学习Visual C#编程时参考使用。
资源详情
资源评论
资源推荐

Part I: The C# Language
264
Partial Method Definitions
Partial classes may also define partial methods. Partial methods are defined in one partial class
definition without a method body, and implemented in another partial class definition. In both places,
the
partial keyword is used:
public partial class MyClass
{
partial void MyPartialMethod();
}
public partial class MyClass
{
partial void MyPartialMethod()
{
// Method implementation
}
}
Partial methods can also be static, but they are always private and can ’ t have a return value. Any
parameters they use can ’ t be
out parameters, although they can be ref parameters. They also can ’ t use
the
virtual , abstract , override , new , sealed , or extern modifier.
Given these limitations, it is not immediately obvious what purpose partial methods fulfill. In fact, they
are important when it comes to code compilation, rather than usage. Consider the following code:
public partial class MyClass
{
partial void DoSomethingElse();
public void DoSomething()
{
Console.WriteLine(“DoSomething() execution started.”);
DoSomethingElse();
Console.WriteLine(“DoSomething() execution finished.”);
}
}
public partial class MyClass
{
partial void DoSomethingElse()
{
Console.WriteLine(“DoSomethingElse() called.”);
}
}
c10.indd 264c10.indd 264 3/24/08 3:36:52 PM3/24/08 3:36:52 PM

Chapter 10: Defi ning Class Members
265
Here, the partial method DoSomethingElse is defined and called in the first partial class definition,
and implemented in the second. The output, when
DoSomething is called from a console application, is
what you might expect:
DoSomething() execution started.
DoSomethingElse() called.
DoSomething() execution finished.
If you were to remove the second partial class definition or partial method implementation entirely (or
comment out the code), the output would be as follows:
DoSomething() execution started.
DoSomething() execution finished.
You might assume that what is happening here is that when the call to DoSomethingElse is made, the
runtime discovers that the method has no implementation and therefore continues executing the next
line of code. What actually happens is a little subtler. When you compile code that contains a partial
method definition without an implementation, the compiler actually removes the method entirely. It also
removes any calls to the method. When you execute the code, no check is made for an implementation
because there is no call to check. This results in a slight — but nevertheless significant — improvement in
performance.
As with partial classes, partial methods are useful when it comes to customizing autogenerated or
designer - created code. The designer may declare partial methods that you can choose to implement or
not depending on the situation. If you don ’ t implement them, you incur no performance hit because
effectively the method does not exist in the compiled code.
Consider at this point why partial methods can ’ t have a return type. If you can answer that to your own
satisfaction, you can be sure that you fully understand this topic — so that is left as an exercise for you.
Example Application
To illustrate some of the techniques you ’ ve been using so far, in this section you ’ ll develop a class
module that you can build on and make use of in subsequent chapters. The class module will contain
two classes:
Card — Representing a standard playing card, with a suit of club, diamond, heart, or spade, and
a rank that lies between ace and king .
Deck — Representing a full deck of 52 cards, with access to cards by position in the deck and the
ability to shuffle the deck .
You ’ ll also develop a simple client to ensure that things are working, but you won ’ t use the deck in a full
card game application — yet.
❑
❑
c10.indd 265c10.indd 265 3/24/08 3:36:52 PM3/24/08 3:36:52 PM

Part I: The C# Language
266
Planning the Application
The class library for this application, Ch10CardLib, will contain your classes. Before you get down to any
code, though, you should plan the required structure and functionality of your classes.
The Card Class
The Card class is basically a container for two read - only fields: suit and rank . The reason for making
the fields read - only is that it doesn ’ t make sense to have a “ blank ” card, and cards shouldn ’ t be able to
change once they have been created. To facilitate this, you ’ ll make the default constructor private, and
provide an alternative constructor that builds a card from a supplied suit and rank.
Other than that, the
Card class will override the ToString method of System.Object , so that you can
easily obtain a human - readable string representing the card. To make things a little simpler, you ’ ll
provide enumerations for the two fields
suit and rank .
The
Card class is shown in Figure 10 - 8 .
+suit
+rank
+ToString()
Card
Figure 10-8
0...*
1
Card
⫹suit
⫹rank
⫹ToString()
Deck
⫺cards : Card[]
⫹GetCard()
⫹Deck()
⫹Shuffle()
Figure 10-9
The Deck Class
The Deck class will maintain 52 Card objects. You can just use a simple array type for this. This array
won ’ t be directly accessible because access to the
Card object is achieved through a GetCard method,
which returns the
Card object with the given index. This class should also expose a Shuffle method to
rearrange the cards in the array. The
Deck class is shown in Figure 10 - 9 .
c10.indd 266c10.indd 266 3/24/08 3:36:53 PM3/24/08 3:36:53 PM

Chapter 10: Defi ning Class Members
267
Writing the Class Library
For the purposes of this example, it is assumed that you are familiar enough with the IDE to bypass the
standard Try It Out format, so the steps aren ’ t listed explicitly, as they are the same steps you ’ ve used
many times. The important thing here is a detailed look at the code. Nonetheless, several pointers are
included to ensure that you don ’ t run into any problems along the way.
Both your classes and your enumerations will be contained in a class library project called Ch10CardLib.
This project will contain four
.cs files: Card.cs , which contains the Card class definition, Deck.cs ,
which contains the
Deck class definition, and Suit.cs and Rank.cs files containing enumerations.
You can put together a lot of this code using the VS class diagram tool.
Don ’ t worry if you are using VCE and don ’ t have the class diagram tool at your disposal. Each of the
following sections also includes the code generated by the class diagram, so you ’ ll be able to follow along
just fine. There are no differences in the code for this project between the IDEs.
To get started, you need to do the following:
1. Create a new class library project called Ch10CardLib and save it in the directory
C:\BegVCSharp\Chapter10.
2. Remove Class1.cs from the project.
3. If you are using VS, open the class diagram for the project using the Solution Explorer window
(you must have the project selected, rather than the solution, for the class diagram icon to
appear). The class diagram should be blank to start with because the project contains no classes.
If you can see the Resources and Settings classes in this view, they can be hidden by right - clicking on
them and selecting Remove from Diagram.
Adding the Suit and Rank Enumerations
You can add an enumeration to the class diagram by dragging an Enum from the Toolbox into the
diagram, and then filling in the dialog that appears. For example, for the
Suit enumeration, fill out the
dialog as shown in Figure 10 - 10 .
Figure 10-10
c10.indd 267c10.indd 267 3/24/08 3:36:53 PM3/24/08 3:36:53 PM

Part I: The C# Language
268
Next, add the members of the enumeration using the Class Details window. The values required are
shown in Figure 10 - 11 .
Figure 10-11
Figure 10-12
Add the
Rank enumeration from the Toolbox in the same way. The values required for the Rank
enumeration are shown in Figure 10 - 12 .
The value entry for the first member is
Ace so that the underlying storage of the enum matches the rank
of the card, such that Six is stored as 6, for example.
When you ’ ve finished, the diagram should look as shown in Figure 10 - 13 .
Figure 10-13
c10.indd 268c10.indd 268 3/24/08 3:36:53 PM3/24/08 3:36:53 PM
剩余299页未读,继续阅读











安全验证
文档复制为VIP权益,开通VIP直接复制

评论1