楼主: 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
151#
 楼主| 发表于 2006-8-13 21:52 | 只看该作者
Module 10: Inheritance in C# 45
Using Abstract Classes in a Class Hierarchy (continued)
interface IToken
{
string Name( );
}
abstract class Token
{
public virtual string Name( )
{ ...
}
...
}
class CommentToken: Token, IToken
{ ...
}
class KeywordToken: Token, IToken
{ ...
}
interface IToken
{
string Name( );
}
abstract class Token
{
public virtual string Name( )
{ ...
}
...
}
class CommentToken: Token, IToken
{ ...
}
class KeywordToken: Token, IToken
{ ...
}
n Example 2
Token
{ abstract }
Token
{ abstract }
IToken
? interface ?
IToken
? interface ?
Comment
Token
? concrete ?
Comment
Token
? concrete ?
Keyword
Token
? concrete ?
Keyword
Token
? concrete ?
To continue the discussion of the role played by abstract classes in a classic
three-tier hierarchy, another example is presented in the slide.
An Abstract Class That Does Not Implement an Interface
Consider Example 2, which appears on the slide. In this example, the abstract
class does not implement the interface. This means that the only way it can
supply an interface implementation to a further derived concrete class is by
providing a public method. The method definition in the abstract class is
optionally virtual, so it can be overridden in the classes as shown in the
following code:
interface IToken
{
string Name( );
}
abstract class Token
{
public virtual string Name( ) { ... }
}
class CommentToken: Token, IToken
{
public override string Name( ) { ... } // Okay
}
This shows that a class can inherit its interface and its implementation of that
interface from separate branches of the inheritance.
46 Module 10: Inheritance in C#
Comparing Abstract Classes to Interfaces
n Similarities
l Neither can be instantiated
l Neither can be sealed
n Differences
l Interfaces cannot contain any implementation
l Interfaces cannot declare non-public members
l Interfaces cannot extend non-interfaces
Both abstract classes and interfaces exist to be derived from (or implemented).
However, a class can extend at most one abstract class, so you need to be more
careful when deriving from an abstract class than you need to be when deriving
from an interface. Reserve the use of abstract classes for implementing true “is
a” relationships.
The similarities between abstract classes and interfaces are that they:
n Cannot be instantiated.
This means that they cannot be used directly to create objects.
n Cannot be sealed.
This is acceptable because if an interface is sealed, it cannot not be
implemented.
The differences between abstract classes and interfaces are summarized in the
following table.
Interfaces Abstract classes
Cannot contain implementation Can contain implementation
Cannot declare non-public members Can declare non -public members
Can extend only other interfaces Can extend other classes, which can be
non-abstract
When comparing the similarities and differences between abstract classes and
interfaces, think of abstract classes as unfinished classes that contain plans for
what needs to be finished.

使用道具 举报

回复

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

本版积分规则 发表回复

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