当前位置: 代码迷 >> 综合 >> jscript写类
  详细解决方案

jscript写类

热度:0   发布时间:2024-01-21 18:52:29.0
题目:运用JAVASCRIPT,写一个类,类名:student,他的属性:name,age,tall,他的方法:getName,getAge,getTall

//首先定义函数
 function student_getName()
    {
        return this.name;
    }
    function student_getAge()
    {
        return this.age;
    }
    function student_getTall()
    {
        return this.tall;
    }
    //定义构造函数
    function student(n,a,t)
    {
        //初始化对象的属性
        this.name=n;
        this.age=a;
        this.tall=t;
        //定义对象的方法
        this.getName=student_getName;
        this.getAge=student_getAge;
        this.getTall=student_getTall;
    }
   function test()
   {
    //首先定义函数
   var s=new student("张三",25,175);
    alert(s.getName());
    alert(s.getAge());
    alert(s.getTall());
    }  
  相关解决方案