楼主: bnso

Introduction to C# Programming

[复制链接]
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
141#
 楼主| 发表于 2006-8-13 21:47 | 只看该作者
20 Module 10: Inheritance in C#
Practice: Implementing Methods
class A {
public virtual void M() { Console.Write("A"; }
}
class B: A {
public override void M() { Console.Write("B"; }
}
class C: B {
new public virtual void M() { Console.Write("C"; }
}
class D: C {
public override void M() { Console.Write("D"; }
static void Main() {
D d = new D(); C c = d; B b = c; A a = b;
d.M(); c.M(); b.M(); a.M();
}
}
class A {
public virtual void M() { Console.Write("A"; }
}
class B: A {
public override void M() { Console.Write("B"; }
}
class C: B {
new public virtual void M() { Console.Write("C"; }
}
class D: C {
public override void M() { Console.Write("D"; }
static void Main() {
D d = new D(); C c = d; B b = c; A a = b;
d.M(); c.M(); b.M(); a.M();
}
}
To practice the use of the virtual, override and new keywords, work through
the code displayed on this slide to figure out what the output of the code will be.
The Solution
After the program executes, it will display the result DDBB to the console.
Program Logic
There is only one object created by the program. This is the object of type D
created in the following declaration:
D d = new D( );
The remaining declaration statements in Main declare variables of different
types that all refer to this one object:
n c is a C reference to d.
n b is a B reference to c, which is reference to d.
n a is an A reference to b, which is reference to c, which is reference to d.
Then come the four expression statements. The following text explains each
one individually.
The first statement is
d.M( )
This is a call to D.M, which is declared override and hence is implicitly virtual.
This means that at run time the compiler calls the most derived implementation
of D.M in the object of type D. This implementation is D.M, which writes D to
the console.
Module 10: Inheritance in C# 21
The second statement is
c.M( )
This is a call to C.M, which is declared virtual. This means that at run time the
compiler calls the most derived implementation of C.M in the object of type D.
Since D.M overrides C.M, D.M is the most derived implementation, in this
case. Hence D.M is called, and it writes D to the console again.
The third statement is
b.M( )
This is a call to B.M, which is declared override and hence is implicitly virtual.
This means that at run time the compiler calls the most derived implementation
of B.M in the object of type D. Since C.M does not override B.M but
introduces a new method that hides C.M, the most derived implementation of
B.M in the object of type D is B.M. Hence B.M is called, and it writes B to the
console.
The last statement is
a.M( )
This is a call to A.M, which is declared virtual. This means that at run time the
compiler calls the most derived implementation of A.M in the object of type D.
B.M overrides A.M, but as before C.M does not override B.M. Hence the most
derived implementation of A.M in the object of type D is B.M. Hence B.M is
called, which writes B to the console again.
This is how the program generates the output DDBB and writes it to the console.
In this example, the C and D classes contain two virtual methods that have the
same signature: the one introduced by A and the one introduced by C. The
method introduced by C hides the method introduced by A. Thus, the override
declaration in D overrides the method introduced by C, and it is not possible for
D to override the method introduced by A.

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
142#
 楼主| 发表于 2006-8-13 21:47 | 只看该作者
22 Module 10: Inheritance in C#
This page intentionally left blank.
Module 10: Inheritance in C# 23
Quiz: Spot the Bugs
class Base
{
public void Alpha( ) { ... }
public virtual void Beta( ) { ... }
public virtual void Gamma(int i) { ... }
public virtual void Delta( ) { ... }
private virtual void Epsilon( ) { ... }
}
class Derived: Base
{
public override void Alpha( ) { ... }
protected override void Beta( ) { ... }
public override void Gamma(double d) { ... }
public override int Delta( ) { ... }
}
class Base
{
public void Alpha( ) { ... }
public virtual void Beta( ) { ... }
public virtual void Gamma(int i) { ... }
public virtual void Delta( ) { ... }
private virtual void Epsilon( ) { ... }
}
class Derived: Base
{
public override void Alpha( ) { ... }
protected override void Beta( ) { ... }
public override void Gamma(double d) { ... }
public override int Delta( ) { ... }
}
In this quiz, you can work with a partner to spot the bugs in the code on the
slide. To see the answers to this quiz, turn the page.
24 Module 10: Inheritance in C#
Answers
The following errors occur in this code:
1. The Base class declares a private virtual method called Epsilon. Private
methods cannot be virtual. The C# compiler traps this bug as a compile-time
error. You can correct the code as follows:
class Base
{
...
public virtual void Epsilon( ) { ... }
}
You can also correct the code in this manner:
class Base
{
...
private void Epsilon( ) { ... } // Not virtual
}
2. The Derived class declares the Alpha method with the override modifier.
However, the Alpha method in the base class is not marked virtual. You can
only override a virtual method. The C# compiler traps this bug as a compiletime
error. You can correct the code as follows:
class Base
{
public virtual void Alpha( ) { ... }
...
}
You can also correct the code in this manner:
class Derived: Base
{
/*any*/ new void Alpha( ) { ... }
...
}
3. The Derived class declares a protected method called Beta with the
override modifier. However, the base class method Beta is public. When
overriding a method, you cannot change its access. The C# compiler traps
this bug as a compile-time error. You can correct the code as follows:
class Derived: Base
{
...
public override void Beta( ) { ... }
...
}

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
143#
 楼主| 发表于 2006-8-13 21:47 | 只看该作者
Module 10: Inheritance in C# 25
You can also correct the code in this manner:
class Derived: Base
{
...
/* any access */ new void Beta( ) { ... }
...
}
4. The Derived class declares a public method called Gamma with the
override modifier. However, the base class method called Gamma and the
Derived class method called Gamma take different parameters. When
overriding a method, you cannot change the parameter types. The C#
compiler traps this bug as a compile-time error. You can correct the code as
follows:
class Derived: Base
{
...
public virtual void Gamma(int i) { ... }
}
You can also correct the code in this manner:
class Derived: Base
{
...
/* any access */ void Gamma(double d) { ... }
...
}
5. The Derived class declares a public method called Delta with the override
modifier. However, the base class method called Delta and the derived class
method called Delta return different types. When overriding a method, you
cannot change the return type. The C# compiler traps this bug as a compiletime
error. You can correct the code as follows:
class Derived: Base
{
...
public override int Delta( ) { ... }
}
You can also correct the code in this manner:
class Derived: Base
{
...
/* any access */ new int Delta( ) { ... }
...
}
26 Module 10: Inheritance in C#
Using Sealed Classes
n You Cannot Derive from a Sealed Class
n You Can Use Sealed Classes for Optimizing Operations
at Run Time
n Many .NET Classes Are Sealed: String, StringBuilder,
and so on
n Syntax: Use the sealed Keyword
namespace System
{
public sealed class String
{
...
}
}
namespace Mine
{
class FancyString: String { ... }
}
namespace System
{
public sealed class String
{
...
}
}
namespace Mine
{
class FancyString: String { ... }
} ?
Creating a flexible inheritance hierarchy is not easy. Most classes are
standalone classes and are not designed to have other classes derived from them.
However, in terms of the syntax, deriving from a class is very easy and the
procedure involves only a few keystrokes. This ease of derivation creates a
dangerous opportunity for programmers to derive from a class that is not
designed to act as a base class.
To alleviate this problem and to better express the programmers’ intentions to
the compiler and to fellow programmers, C# allows a class to be declared
sealed. You cannot derive from a sealed class.

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
144#
 楼主| 发表于 2006-8-13 21:48 | 只看该作者
Module 10: Inheritance in C# 27
Keyword Syntax
You can seal a class by using the sealed keyword. The syntax for this keyword
is as shown:
namespace System
{
public sealed class String
{
...
}
}
There are many examples of sealed classes in the Microsoft? .NET Framework.
The slide shows the System.String class, where the keyword string is an alias
for this class. This class is sealed, and so you cannot derive from it.
Optimizing Operations at Run Time
The sealed modifier enables certain run-time optimizations. In particular,
because a sealed class is known to never have any derived classes, it is possible
to transform virtual function member calls on sealed class instances into nonvirtual
function member calls.
28 Module 10: Inheritance in C#
u Using Interfaces
n Declaring Interfaces
n Implementing Multiple Interfaces
n Implementing Interface Methods
n Implementing Interface Methods Explicitly
n Quiz: Spot the Bugs
An interface specifies a syntactic and semantic contract that all derived classes
must adhere to. Specifically, an interface describes the what part of the contract
and the classes that implement the interface describe the how part of the
contract.
In this section, you will learn the syntax for declaring interfaces and the two
techniques for implementing interface methods in derived classes.
Module 10: Inheritance in C# 29
Declaring Interfaces
n Syntax: Use the interface Keyword to Declare Methods
interface IToken
{
int LineNumber( );
string Name( );
}
interface IToken
{
int LineNumber( );
string Name( );
}
IToken
? interface ?
IToken
? interface ?
LineNumber( )
Name( )
LineNumber( )
Name( )
NNNooo m mmeeettthhoodd bbbooddiieesss
Interface names should
begin with a capital “I”
IInntteerrffaaccee nnaammeess sshhoouulldd
bbeeggiinn wwiitthh aa ccaappiittaall ““II””
NNNooo a acccceessss ssppeecciiffiieerrss
An interface resembles a class without any code. You declare an interface in a
similar manner to the way in which you declare a class. To declare an interface
in C#, you use the keyword interface instead of class. The syntax for this
keyword is explained on the slide.
It is recommended that all interface names be prefixed with a capital "I."
For example, use IToken rather than Token.
Note

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
145#
 楼主| 发表于 2006-8-13 21:48 | 只看该作者
30 Module 10: Inheritance in C#
Features of Interfaces
The following are two important features of interfaces.
Interface Methods Are Implicitly Public
The methods declared in an interface are implicitly public. Therefore, explicit
public access modifiers are not allowed, as shown in the following example:
interface IToken
{
public int LineNumber( ); // Compile-time error
}
Interface Methods Do Not Contain Method Bodies
The methods declared in an interface are not allowed to contain method bodies.
For example, the following code is not allowed:
interface IToken
{
int LineNumber( ) { ... } // Compile-time error
}
Strictly speaking, interfaces can contain interface property declarations, which
are declarations of properties with no body, interface event declarations, which
are declarations of events with no body, and interface indexer declarations,
which are declarations of indexers with no body.
Module 10: Inheritance in C# 31
Implementing Multiple Interfaces
n A Class Can Implement Zero or More Interfaces
n An Interface Can Extend Zero or More Interfaces
n A Class Can Be More Accessible Than Its Base Interfaces
n An Interface Cannot Be More Accessible Than Its Base Interfaces
n A Class Must Implement All Inherited Interface Methods
interface IToken
{
string Name( );
}
interface IVisitable
{
void Accept(IVisitor);
}
class Token: IToken, IVisitable
{ ...
}
interface IToken
{
string Name( );
}
interface IVisitable
{
void Accept(IVisitor);
}
class Token: IToken, IVisitable
{ ...
}
IToken
? interface ?
IToken
? interface ?
IVisitable
? interface ?
IVisitable
? interface ?
Token
? concrete ?
Token
? concrete ?
Although C# permits only single inheritance, it allows you to implement
multiple interfaces in a single class. This topic discusses the differences
between a class and an interface with respect to implementation and extension
of interfaces, respectively, in addition to their accessibility in comparison to
their base interfaces.
Interface Implementation
A class can implement zero or more interfaces but can explicitly extend no
more than one class. An example of this feature is displayed on the slide.
Strictly speaking, a class always extends one class. If you do not specify
a base class, your class will implicitly inherit from object.
Note

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
146#
 楼主| 发表于 2006-8-13 21:48 | 只看该作者
32 Module 10: Inheritance in C#
In contrast, an interface can extend zero or more interfaces. For example, you
can rewrite the code on the slide as follows:
interface IToken { ... }
interface IVisitable { ... }
interface IVisitableToken: IVisitable, IToken { ... }
class Token: IVisitableToken { ... }
Accessibility
A class can be more accessible than its base interfaces. For example, you can
declare a public class that implements a private interface, as follows:
class Example
{
private interface INested { }
public class Nested: INested { } // Okay
}
However, an interface cannot be more accessible than any of its base interfaces.
It is an error to declare a public interface that extends a private interface, as
shown in the following example:
class Example
{
private interface INested { }
public interface IAlsoNested: INested { }
// Compile-time error
}
Interface Methods
A class must implement all methods of any interfaces it extends, regardless of
whether the interfaces are inherited directly or indirectly.
Module 10: Inheritance in C# 33
Implementing Interface Methods
n The Implementing Method Must Be the Same As the
Interface Method
n The Implementing Method Can Be virtual or Non-Virtual
class Token: IToken, IVisitable
{
public virtual string Name( )
{ ...
}
public void Accept(IVisitor v)
{ ...
}
}
class Token: IToken, IVisitable
{
public virtual string Name( )
{ ...
}
public void Accept(IVisitor v)
{ ...
}
}
Same access
Same return type
Same name
Same parameters
SSaamee aaccess
SSaammee rretturrn type
SSaammee nnaammee
SSaammee ppaarraammeetteerrss
When a class implements an interface, it must implement every method
declared in that interface. This requirement is practical because interfaces
cannot define their own method bodies.
The method that the class implements must be identical to the interface method
in every way. It must have the same:
n Access
Since an interface method is implicitly public, this means that the
implementing method must be explicitly declared public. If the access
modifier is omitted, then the method defaults to being private.
n Return type
If the return type in the interface is declared as T, then the return type in
the implementing class cannot be declared as a type derived from T; it must
be T. In other words, return type covariance is not supported in C#.
n Name
Remember that names in C# are case sensitive.
n Parameter-type list

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
147#
 楼主| 发表于 2006-8-13 21:49 | 只看该作者
34 Module 10: Inheritance in C#
The following code meets all of these requirements:
interface IToken
{
string Name( );
}
interface IVisitable
{
void Accept(IVisitor);
}
class Token: IToken, IVisitable
{
public virtual string Name( )
{ ...
}
public void Accept(IVisitor v)
{ ...
}
}
The implementing method can be virtual, such as Name in the preceding code.
In this case, the method can be overridden in further derived classes. The
implementing method can also be non-virtual, such as Accept in the preceding
code. In the latter case, the method cannot be overridden in further derived
classes.
Module 10: Inheritance in C# 35
Implementing Interface Methods Explicitly
n Use the Fully Qualified Interface Method Name
n Restrictions of Explicit Interface Method Implementation
l You can only access methods through the interface
l You cannot declare methods as virtual
l You cannot specify an access modifier
class Token: IToken, IVisitable
{
string IToken.Name( )
{ ...
}
void IVisitable.Accept(IVisitor v)
{ ...
}
}
class Token: IToken, IVisitable
{
string IToken.Name( )
{ ...
}
void IVisitable.Accept(IVisitor v)
{ ...
}
}
An alternative way for a class to implement a method inherited from an
interface is to use an explicit interface method implementation.
Use the Fully Qualified Interface Method Name
When using the explicit interface method implementation, you must use the
fully qualified name of the implementing method. This implies that the name of
the method must include the name of the interface as well, such as IToken in
IToken.Name.
An example of two interface methods implemented explicitly by the Token
class is displayed on the slide. Notice the differences between this
implementation and the earlier implementation.
36 Module 10: Inheritance in C#
Restrictions of Explicit Interface Method Implementation
When implementing explicit interfaces, you need to be aware of certain
restrictions.
You Can Only Access Methods Through the Interface
You cannot access an explicit interface method implementation from anywhere
except through the interface. This is shown in the following example:
class Token: IToken, IVisitable
{
string IToken.Name( )
{
...
}
private void Example( )
{
Name( ); // Compile-time error
((IToken)this).Name( ); // Okay
}
}
You Cannot Declare Methods As Virtual
In particular, a further derived class cannot access an explicit interface method
implementation, and, as a result, no method can override it. This implies that an
explicit interfac e method implementation is not virtual and cannot be declared
virtual.
You Cannot Specify an Access Modifier
When defining an explicit interface method implementation, you cannot specify
an access modifier. This is because explicit interface member implementations
have different accessibility characteristics than other methods.
No Direct Access
An explicit interface method implementation is not directly accessible to clients
and in this sense is private. This is shown in the following code:
class InOneSensePrivate
{
void Method(Token t)
{
t.Name( ); // Compile-time error
}
}

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
148#
 楼主| 发表于 2006-8-13 21:49 | 只看该作者
Module 10: Inheritance in C# 37
Indirect Access Through Interface Variable
An explicit interface method implementation is indirectly accessible to clients
by means of an interface variable and polymorphism. In this sense, it is public.
This is shown in the following code:
class InAnotherSensePublic
{
void Method(Token t)
{
((IToken)t).Name( ); // Okay
}
}
Advantages of an Explicit Implementation
Explicit interface member implementations serve two primary purposes:
1. They allow interface implementations to be excluded from the public
interface of a class or struct. This is useful when a class or struct
implements an internal interface that is of no interest to the class or struct
user.
2. They allow a class or struct to provide different implementations for
interface methods that have the same signature. Following is an example:
interface IArtist
{
void Draw( );
}
interface ICowboy
{
void Draw( );
}
class ArtisticCowboy: IArtist, ICowboy
{
void IArtist.Draw( )
{
...
}
void ICowboy.Draw( )
{
...
}
}
38 Module 10: Inheritance in C#
This page intentionally left blank.
Module 10: Inheritance in C# 39
Quiz: Spot the Bugs
interface IToken
{
string Name( );
int LineNumber( ) { return 42; }
string name;
}
class Token
{
public string IToken.Name( ) { ... }
static void Main( )
{
IToken t = new IToken( );
}
}
interface IToken
{
string Name( );
int LineNumber( ) { return 42; }
string name;
}
class Token
{
public string IToken.Name( ) { ... }
static void Main( )
{
IToken t = new IToken( );
}
}
In this quiz, you can work with a partner to spot the bugs in the code on the
slide. To see the answers to this quiz, turn the page.

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
149#
 楼主| 发表于 2006-8-13 21:49 | 只看该作者
40 Module 10: Inheritance in C#
Answers
The following bugs occur in the code on the slide:
1. The IToken interface declares a method called LineNumber that has a
body. An interface cannot contain any implementation. The C# compiler
traps this bug as a compile-time error. The corrected code is as follows:
interface IToken
{
...
int LineNumber( );
...
}
2. The IToken interface declares a field called name. An interface cannot
contain any implementation. The C# compiler traps this bug as a compiletime
error. The corrected code is as follows:
interface IToken
{
string Name( );
int LineNumber( );
//string name; // Field now commented out
}
3. The Token class contains the explicit interface method implementation
IToken.Name( ) but the class does not specify IToken as a base interface.
The C# compiler traps this bug as a compile-time error. The corrected code
is as follows:
class Token: IToken
{
...
}
4. Now that Token specifies IToken as a base interface, it must implement
both methods declared in that interface. The C# compiler traps this bug as a
compile-time error. The corrected code is as follows:
class Token: IToken
{
public string Name( ) { ... }
public int LineNumber( ) { ... }
...
}
Module 10: Inheritance in C# 41
5. The Token.Main method attempts to create an instance of the interface
IToken. However, you cannot create an instance of an interface. The C#
compiler traps this bug as a compile-time error. The corrected code is as
follows:
class Token: IToken
{
...
static void Main( )
{
IToken t = new Token( );
...
}
}
42 Module 10: Inheritance in C#
u Using Abstract Classes
n Declaring Abstract Classes
n Using Abstract Classes in a Class Hierarchy
n Comparing Abstract Classes to Interfaces
n Implementing Abstract Methods
n Working with Abstract Methods
n Quiz: Spot the Bugs
Abstract classes are used to provide partial class implementations that can be
completed by derived concrete classes. Abstract classes are particularly useful
for providing a partial implementation of an interface that can be reused by
multiple derived classes.
This section describes the syntax for declaring an abstract class, presents some
examples of how you can use abstract classes in a class hierarchy, and
introduces abstract methods.
Module 10: Inheritance in C# 43
Declaring Abstract Classes
n Use the abstract Keyword
abstract class Token
{
...
}
class Test
{
static void Main( )
{
new Token( );
}
}
abstract class Token
{
...
}
class Test
{
static void Main( )
{
new Token( );
}
}
Token
{ abstract }
Token
{ abstract }
An abstract class cannot
be instantiated
AAnn aabbssttrraacctt ccllaassss ccaannnnoott
? bbee iinnssttaannttiiaatteedd
You declare an abstract class by using the keyword abstract, as is shown on the
slide.

使用道具 举报

回复
论坛徽章:
49
NBA季后赛之星
日期:2014-10-19 19:51:33蓝锆石
日期:2014-10-19 19:51:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33指数菠菜纪念章
日期:2014-10-19 19:52:33问答徽章
日期:2014-04-15 10:41:44优秀写手
日期:2014-07-24 06:00:11保时捷
日期:2014-10-19 19:51:33三菱
日期:2014-10-19 19:51:33
150#
 楼主| 发表于 2006-8-13 21:52 | 只看该作者
The rules governing the use of an abstract class are almost exactly the same as
those governing a non-abstract class. The only differences between using
abstract and non-abstract classes are:
n You cannot create an instance of an abstract class.
In this sense, abstract classes are like interfaces.
n You can create an abstract method in an abstract class.
An abstract class can declare an abstract method, but a non-abstract class
cannot.
Common features of abstract classes and non-abstract classes are:
n Limited extensibility
An abstract class can extend at most one other class or abstract class. Note
that an abstract class can extend a non-abstract class, whereas the converse
is not true.
n Multiple interfaces
An abstract class can implement multiple interfaces.
n Inherited interface methods
An abstract class must implement all inherited interface methods.
44 Module 10: Inheritance in C#
Using Abstract Classes in a Class Hierarchy
interface IToken
{
string Name( );
}
abstract class Token: IToken
{
string IToken.Name( )
{ ...
}
...
}
class CommentToken: Token
{ ...
}
class KeywordToken: Token
{ ...
}
interface IToken
{
string Name( );
}
abstract class Token: IToken
{
string IToken.Name( )
{ ...
}
...
}
class CommentToken: Token
{ ...
}
class KeywordToken: Token
{ ...
}
n Example 1
Token
{ abstract }
Token
{ abstract }
IToken
? interface ?
IToken
? interface ?
Comment
Token
? concrete ?
Comment
Token
? concrete ?
Keyword
Token
? concrete ?
Keyword
Token
? concrete ?
The role of abstract classes in a classic three-tier hierarchy, consisting of an
interface, an abstract class, and a concrete class, is to provide a complete or
partial implementation of an interface.
An Abstract Class Implementing an Interface
Consider Example 1, which appears on the slide. In this example, the abstract
class implements an interface. It is an explicit implementation of the interface
method. The explicit implementation is not virtual and therefore cannot be
overridden in the further derived classes, such as CommentToken.
However, it is possible for CommentToken to re-implement the IToken
interface as follows:
interface IToken
{
string Name( );
}
abstract class Token: IToken
{
string IToken.Name( ) { ... }
}
class CommentToken: Token, IToken
{
public virtual string Name( ) { ... }
}
Note that in this case it is not necessary to mark CommentToken.Name as a
new method. This is because a derived class method can hide only a visible
base class method, but the explicit implementation of Name in Token is not
directly visible in CommentToken.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表