使用MicrosoftAJAX实现Javascript面向对象(六)反射

反射

 

反射是指在运行时检查程序的结构和组件的能力。 实现反射的 API 是对 Type 类的扩展。 通过这些方法,可以收集有关对象的信息,例如该对象继承自谁,它是否实现特定的接口,以及它是否是特定类的实例等。


反射的实现 反射方法

反射的prototype方法

  • NamespaceName.ClassName.getBaseType() 得到基类的类型
  • NamespaceName.ClassName.getInterfaces() 得到实现的接口集合
  • NamespaceName.ClassName.getName() 得到类型名
  • NamespaceName.ClassName.implementsInterface(NamespaceName.InterfaceName) 得到一个布尔值,判断是否实现了某个接口,实现了返回true,否则返回false
  • NamespaceName.ClassName.inheritsFrom(Namespace.ClassName) 得到一个布尔值,判断是否继承了某个类,继承了返回true,否则返回false
  • NamespaceName.Interface.isImplementedBy(Namespace.ClassName) 判断一个接口是否被另外一个类实现了,实现了返回true,否则返回false
  • Namespace.ClassName.isInstanceOfType(instance) 判断某个类型的实例是否是这个类型。是返回true,否则返回false 


Type的静态方法

  • Type.getRootNamespaces() 得到根命名空间的集合
  • Type.isClass(Namespace.ClassName) 判断一个类型是否是类,是返回true,否则返回false
  • Type.isNamespace(Namespace) 判断一个类型是否是命名空间
  • Type.isEnum(TypeName) 判断一个类型是否是枚举类型
  • Type.isFlags(TypeName) 判断一个类型是否是标记
  • Type.parse(Namespace.ClassName) 通过反射的方法,按照类名,和命名空间创建一个命名空间下的某个类的实例
  • Object.getType(Instance) 通过实例得到这个实例的类型
  • Object.getTypeName(Instance) 通过实例得到这个实例的类型名

示例 

6-reflection.htm
<! DOCTYPE HTML >
< html >
     < head >
         < title ></ title >
         < meta  http-equiv ="Content-Type"  content ="text/html; charset=utf-8"   />
         <!-- <script src="http://www.cnblogs.com/libs/MicrosoftAjax/MicrosoftAjax.js" type="text/javascript"></script> -->
         < script  src ="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"  type ="text/javascript" ></ script >
         < script  type ="text/javascript" >
            Type.registerNamespace(
" jweaving.tutorials " );

            jweaving.tutorials.Style 
=   function () {
            };

            jweaving.tutorials.Style.prototype 
=  {
                NORMAL : 
1 ,
                BOLD : 
2 ,
                ITALIC : 
3 ,
                UNDERLINE : 
4
            }

            jweaving.tutorials.Style.registerEnum(
" jweaving.tutorials.Style " );

            jweaving.tutorials.TextBlock 
=   function (name, text) {
                
this ._name  =  name;
                
this ._text  =  text;
                
this ._color  =   " #000000 " ;
                
this ._style  =   " normal " ;
            };

            jweaving.tutorials.TextBlock.prototype 
=  {
                getName : 
function () {
                    
return   this ._name
                },
                getText : 
function () {
                    
return   this ._text
                },
                getColor : 
function () {
                    
return   this ._color
                },
                setColor : 
function (color) {
                    
this ._color  =  color
                },
                getStyle : 
function () {
                    
return   this ._style
                },
                setStyle : 
function (style) {
                    
this ._style  =  style
                },
                write : 
function () {
                    
if ( this ._style  ==  jweaving.tutorials.Style.BOLD) {
                        document.write(String.format(
' <div><b name="{0}" style="color:{1};" >{2}</b></div> ' this ._name,  this ._color,  this ._text));
                    } 
else   if ( this ._style  ==  jweaving.tutorials.Style.ITALIC) {
                        document.write(String.format(
' <div><i name="{0}" style="color:{1};" >{2}</i></div> ' this ._name,  this ._color,  this ._text));
                    } 
else   if ( this ._style  ==  jweaving.tutorials.Style.UNDERLINE) {
                        document.write(String.format(
' <div><u name="{0}" style="color:{1};" >{2}</u></div> ' this ._name,  this ._color,  this ._text));
                    } 
else  {
                        document.write(String.format(
' <div name="{0}" style="color:{1};" >{2}</div> ' this ._name,  this ._color,  this ._text));
                    }
                }
            }

            jweaving.tutorials.TextBlock.registerClass(
' jweaving.tutorials.TextBlock ' );

            jweaving.tutorials.Button 
=   function (name, text) {
                
this ._name  =  name;
                
this ._text  =  text;
            };

            jweaving.tutorials.Button.prototype 
=  {
                getName : 
function () {
                    
return   this ._name
                },
                getText : 
function () {
                    
return   this ._text
                },
                toString : 
function () {
                    
return   ' name: '   +   this ._name  +   ' , '   +   ' text: '   +   this ._text;
                }
            }

            jweaving.tutorials.Button.registerClass(
' jweaving.tutorials.Button ' );

            jweaving.tutorials.IAction 
=   function () {
            }

            jweaving.tutorials.IAction.Prototype 
=  {
                show : 
function () {
                },
                execute : 
function (textBlock) {
                }
            }

            jweaving.tutorials.IAction.registerInterface(
' jweaving.tutorials.IAction ' );

            jweaving.tutorials.StyleButton 
=   function (name, text, style) {
                jweaving.tutorials.StyleButton.initializeBase(
this , [name, text]);

                
this ._style  =  style;
            }

            jweaving.tutorials.StyleButton.prototype 
=  {
                getStyle : 
function () {
                    
return   this ._style
                },
                setStyle : 
function (style) {
                    
this ._style  =  style;
                },
                toString : 
function () {
                    
return  jweaving.tutorials.StyleButton.callBaseMethod( this ' toString ' +   ' , '   +   ' style: '   +   this ._style;
                },
                show : 
function () {
                    alert(
' 样式 '   +   this ._style);
                },
                execute : 
function (textBlock) {
                    textBlock.setStyle(
this ._style);
                }
            }

            jweaving.tutorials.StyleButton.registerClass(
' jweaving.tutorials.StyleButton ' , jweaving.tutorials.Button, jweaving.tutorials.IAction);

            jweaving.tutorials.ColorButton 
=   function (name, text, style, color) {
                jweaving.tutorials.ColorButton.initializeBase(
this , [name, text, style]);

                
this ._color  =  color;
            }

            jweaving.tutorials.ColorButton.prototype 
=  {
                getColor : 
function () {
                    
return   this ._color
                },
                setColor : 
function (color) {
                    
this ._color  =  color;
                },
                toString : 
function () {
                    
return  jweaving.tutorials.ColorButton.callBaseMethod( this ' toString ' +   ' , '   +   ' color: '   +   this ._color;
                },
                show : 
function () {
                    alert(
' 颜色 '   +   this ._color);
                },
                execute : 
function (textBlock) {
                    jweaving.tutorials.ColorButton.callBaseMethod(
this ' execute ' , [textBlock]);
                    textBlock.setColor(
this ._color);
                }
            }

            jweaving.tutorials.ColorButton.registerClass(
' jweaving.tutorials.ColorButton ' , jweaving.tutorials.StyleButton);

        
</ script >
     </ head >
     < body >
         < div >
             < p ></ p >
             < input  id ="Button1"  value ="Check Type"
            type
="button"  onclick ="return OnButton1Click()"   />
             < input  id ="Button2"  value ="Check Inheritance"
            type
="button"  onclick ="return OnButton2Click()"   />
             < input  id ="Button3"  value ="Check Interface"
            type
="button"  onclick ="return OnButton3Click()"   />
         </ div >
         < script  type ="text/javascript"  language ="JavaScript" >
            
var  cb  =   new  jweaving.tutorials.ColorButton();
            
var  cbt  =  jweaving.tutorials.ColorButton;
            
var  a  =   new  Array(jweaving.tutorials.Button, jweaving.tutorials.StyleButton, jweaving.tutorials.ColorButton, jweaving.tutorials.IAction, Sys.IContainer);

            
function  OnButton1Click() {
                
for ( var  i  =   0 ; i  <  a.length; i ++ ) {
                    
if (a[i].isInstanceOfType(cb)) {
                        alert(cbt.getName() 
+   "  is a  "   +  a[i].getName()  +   " . " );
                    } 
else
                        alert(cbt.getName() 
+   "  is not a  "   +  a[i].getName()  +   " . " );
                }
            }

            
function  OnButton2Click() {
                
for ( var  i  =   0 ; i  <  a.length; i ++ ) {
                    
if (cbt.inheritsFrom(a[i])) {
                        alert(cbt.getName() 
+   "  inherits from  "   +  a[i].getName()  +   " . " );
                    } 
else
                        alert(cbt.getName() 
+   "  does not inherit from  "   +  a[i].getName()  +   " . " );
                }
            }

            
function  OnButton3Click() {
                
for ( var  i  =   0 ; i  <  a.length; i ++ ) {
                    
if (Type.isInterface(a[i])) {
                        
if (cbt.implementsInterface(a[i])) {
                            alert(cbt.getName() 
+   "  implements the  "   +  a[i].getName()  +   "  interface. " );
                        } 
else
                            alert(cbt.getName() 
+   "  does not implement the  "   +  a[i].getName()  +   "  interface. " );
                    } 
else
                        alert(a[i].getName() 
+   "  is not an interface. " );
                }
            }
        
</ script >
     </ body >
</ html >

 

references

 Microsoft AJAX Library Cheat Sheet——ASP.NET AJAX客户端框架的快速参考系列:http://www.cnblogs.com/dflying/archive/2007/02/09/639638.html

Microsoft AJAX Library Cheat Sheet ——ASP.NET AJAX客户端框架的快速参考:http://www.cnblogs.com/allnen/archive/2009/03/23/1419490.html

在Microsoft AJAX Library下JavaScript的面向对象开发:http://www.cnblogs.com/beniao/archive/2008/06/08/1204388.html

Microsoft Ajax:http://msdn.microsoft.com/zh-cn/library/ee341002.aspx
使用 Microsoft Ajax Library 创建自定义客户端脚本:http://msdn.microsoft.com/zh-cn/library/bb386453.aspx

领先技术: 深入了解 Microsoft AJAX Library:http://msdn.microsoft.com/zh-cn/magazine/cc163300.aspx

Microsoft AJAX Library Cheat Sheets:http://aspnetresources.com/blog/ms_ajax_cheat_sheets_batch2

 ASP.NET AJAX深入浅出系列课程:http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/series/ASP_Ajax.aspx   http://msdnwebcast.net/webcast/4/1957/

 

你可能感兴趣的:(JavaScript)