.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
实战 .Net 数据访问层 - 7

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

 

最后,和大家讨论一个由于引入Def而产生的技术问题。

老规矩,还是先请各位看一段代码:

 

代码6Interface Inheritance下的Def多态问题

public abstract class DefBase : IList, IDictionary

{

    // 既是Interface方法,又被声明为virtual

    public virtual IEnumerator GetEnumerator()

    {

       if (_al != null)

           return _al.GetEnumerator();

       else if (_ht != null)

           return _ht.GetEnumerator();

       else

       {

           // 抛出基类无法处理异常

           throw new Exception(

"Do not handle interface method in DefBase class !");

       }

    }

}

 

public class MyDef: DefBase, IList, IEnumerable

{

    // 既是Interface方法,又被声明为override

    public override IEnumerator GetEnumerator()

    {

       try

       {

           // 先调用DefBaseInterface方法,

           //   如果基类无法处理,截获其抛出的异常

           return base.GetEnumerator();

       }

       catch

       {

           if (this._ostOrm != null)

              return GetList().GetEnumerator();

           else if (this._xmlNode != null)

              return _xmlNode.GetEnumerator();

           else if (this._xmlDoc != null)

              return _xmlDoc.GetEnumerator();

           else

              throw new Exception(

"Do not handle interface method in MyDef class !");

           }

       }

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

不知道注释部分是否已表述清楚:当继承自Interface后,由于还是存在Base ClassDefBase)这样一个事实,MyDef如果要扩展这个Interface实现,就不得不进行virtual / override声明!

同时,由于MyDef实例也存在“仅使用DefBase Interface Implementation足矣”这种情况(例如:Entity Type就是ArrayListHashtable),促使我们不得不采用一些非常手段进行调理!

 

这里,作者采用了异常处理的方法进行判断(有点取巧的味道),一旦基类DefBase无法处理,就直接throw exception(如果考虑全面点,还需事先定义exception type以进行过滤处理),这样层层往上推进,如果最后进行catch的类依然无法处理,那就真的是系统异常了!

还有一种做法稍微复杂点:在DefBase中可以返回null并在MyDef中进行判断,不过,对于不返回任何值或返回值为ValueTypeInterface Method,就必须另辟蹊径了(例如:单独定义一个Null Type Class进行处理,类似.NET Framework中的System.DBNull)!

 

下一段:http://www.csdn.net/develop/Read_Article.asp?id=27550




相关文章

相关软件