Asp.net Ajax 客户端编程(二)——Type 类,面向对象编程的开始

    微软的ASP.NET AJAX能写出丰富的,可交互的JavaScript程序,我们可以通过Type类来注册命名空间,类,枚举等,反射对象的内部性质,Type类是Asp.net Ajax的一个很重要类。
    Type.registerNamespace(namespacePath)  注册一个命名空间。
    Type.isNamespace(object)  判断一个对象是否是命名空间。
    classInstanceVar.registerClass(typeName, baseType, interfaceTypes)   注册一个类,typeName,要注册的类名,baseType继承的类,可以为空,interfaceTypes,继承的接口,可以为空。
    Type.isClass(type)  判断一个对象是否是一个类。
    typeInstanceVar.getName()  获取一个对象的类型名。typeInstanceVar是一个类型,可以用Object.getType(对象实例)来获取。
    TypeA.isInstanceOfType(instance) 判断一个实例是否是某个类的实例。
    typeInstanceVar.registerInterface(typeName)  注册一个接口。
    Type.isInterface(type)    判断一个类型是否是接口。    
    aType.isImplementedBy(typeInstanceVar)  判断一个类是否实现了接口。typeInstanceVar为类实例
    typeInstanceVar.implementsInterface(interfaceType)  也是判断一个类是否实现了接口,interfaceType为要判断的接口
<script language="javascript" type="text/javascript">
         
//Demo:http://young18.cnblogs.com
         //注册命名空间
         Type.registerNamespace("Arcadia");
         
         Arcadia.IStudent 
= function(){  }
         Arcadia.IStudent.Prototype 
= {
                    Study: 
function(){}
                }

         
//注册接口
         Arcadia.IStudent.registerInterface('Arcadia.IStudent');
         
         
         Arcadia.Student 
= function(name, age) {
                
this._name = name;
                
this._age = age;
             }

         Arcadia.Student.prototype 
= {
               GetName:
function(){
                   
return this._name;
               }
,
               GetAge:
function(){
                   
return this._age;
               }
,
               ToString:
function(){
                   
return "Name:"+this._name+"\nAge:"+this._age;
               }
,
               Study: 
function()return "好好学习"; }
           }
         
         Arcadia.Student.registerClass(
"Arcadia.Student",null,Arcadia.IStudent);
         
         
if(Type.isNamespace(Arcadia))
            alert(
"Arcadia是命名空间");
           
         
if(Type.isInterface(Arcadia.IStudent))
            alert(
"Arcadia.IStudent是接口");
         
         
if(Type.isClass(Arcadia.Student))
            alert(
"Arcadia.Student是一个类");        
         
         
var young = new Arcadia.Student("Young",24);
         
         alert(
"young的类名是:" + Object.getType(young).getName());
         
         
if(Arcadia.Student.isInstanceOfType(young))
            alert(
"young是Arcadia.Student的一个实例");
         
         
if(Arcadia.Student.implementsInterface(Arcadia.IStudent))
            alert(
"Arcadia.Student实现了接口Arcadia.IStudent");
    
</script>
    TypeA.inheritsFrom(parentType)  判断类TypeA是否继承自基类parentType。
    instanceVar.callBaseMethod(instance, name, baseArguments)  调用基类方法,instance 为instanceVar的类型,通常为this,name 为要调用的方法名,baseArguments为调用时传入的参数(数组)。
     typeInstanceVar.getBaseType() 获取基类类型
     typeVar.getInterfaces()  获取一个基类继承的所有接口。
     Type.getRootNamespaces()   获取当前客户端应用程序的所有根命名空间。
     typeVar.baseClassName.initializeBase(instance, baseArguments )   实例化基类,instance为要实例化的基类名,baseArguments 为实例化时传递的参数。
<form id="form1" runat="server">
   
<%--Demo:http://young18.cnblogs.com--%>
   <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
    
<script language="javascript" type="text/javascript">
        
//注册一个命名空间
        Type.registerNamespace("Arcadia");
        Arcadia.Student 
= function(name, age) {
                     
this._name = name;
                     
this._age = age;
                }

        Arcadia.Student.prototype 
= {
                    SetName:
function(name){
                        
this._name = name;
                    }
,
                    GetName:
function(){
                        
return this._name;
                    }
,
                    SetAge:
function(age){
                        
this._age = age;
                    }
,
                    GetAge:
function(){
                        
return this._age;
                    }
,
                    ToString:
function(){
                         
return "Name:"+this._name+"\nAge:"+this._age;
                    }

                }

        
//在Arcadia命名空间下注册一个Student类
        Arcadia.Student.registerClass("Arcadia.Student");
        
        
        
//定义一个Student类的子类GirlStudent
        Arcadia.GirlStudent = function(name, age){
                    
//实例化基类,且其基类属性在GirlStudent中被继承。
                    Arcadia.GirlStudent.initializeBase(this, [name, age]);
                    
this._sex="female";
        }

        Arcadia.GirlStudent.prototype 
= {
                    
//设置性别的只读属性
                    GetSex:function(){
                        
return this._sex;
                    }
,
                    ToString:
function(){
                        
//调用基类方法来重写ToString方法。
                        return Arcadia.GirlStudent.callBaseMethod(this, 'ToString') + "\nSex:美女";
                }

        }
        
        
//注册GirlStudent类,包括和Student的继承关系。
        Arcadia.GirlStudent.registerClass("Arcadia.GirlStudent",Arcadia.Student);
        
       
        
//下面方法类测试上面的对象。
        //分别实例化Student和GirlStudent对象。
        var student=new Arcadia.Student("Young",25);
        
var girlstudent=new Arcadia.GirlStudent("xiaoxue",23);
        
        
//调用实例方法。
        function TestStudent()
        
{
            alert(student.ToString());
        }

        
//改变属性
        function TestChangeStudent()
        
{
            alert(
"改变前:\n"+student.ToString());
            student.SetName(
"Jiang");
            alert(
"改变后:\n"+student.ToString());
        }

        
//用inheritsFrom判断继承关系。
        function TestIfInheritFromStudent()
        
{
            
var isInherit = Arcadia.GirlStudent.inheritsFrom(Arcadia.Student);
            
if(isInherit)
                alert(
"女学生类是从学生类中继承");
            
else
                alert(
"女学生类没有继承自学生类");

        }

        
//测试子类重写后的方法。
        function TestGirlStudent()
        
{
            alert(girlstudent.ToString());
        }

    
</script>
    
<a href="javascript:TestStudent()">测试学生类</a><br />
    
<a href="javascript:TestChangeStudent()">改变学生姓名</a><br />
    
<a href="javascript:TestIfInheritFromStudent()">判断女学生类是否继承自学生类</a><br />
    
<a href="javascript:TestGirlStudent()">测试女学生类</a>
    
</form>

ANamespace.AnEnum.registerEnum(name, flags)  注册一个枚举
Arcadia.Sex = function(){};
            Arcadia.Sex.prototype 
= 
            
{
                boy:    
"",
                girl:   
""
            }

         Arcadia.Sex.registerEnum(
"Arcadia.Sex");         
         alert(Arcadia.Sex.boy);
posted @ 2007-05-11 02:25  Young.Jiang  阅读(685)  评论(0编辑  收藏  举报