使用JS编写一段面向对象的代码

来源:互联网 发布:红楼梦贾迎春知乎 编辑:程序博客网 时间:2024/06/06 01:59
[javascript] view plaincopyprint?
  1. <script type="text/javascript">  
  2.  function Animal(aname,age,weight){  
  3.     this.aname = aname;  
  4.     this.age = age;  
  5.     this.weight = weight;  
  6.  }  
  7.  function Animal.prototype.getAname(){  
  8.     return this.aname;  
  9.  }  
  10.  function Animal.prototype.setAname(aname){  
  11.     this.aname = aname;  
  12.  }  
  13.  function Animal.prototype.getAge(){  
  14.    return this.age;   
  15.  }  
  16.  function Animal.prototype.setAage(age){  
  17.    this.age = age;    
  18.  }  
  19.  function Animal.prototype.getWeight(){  
  20.    return this.weight;    
  21.  }  
  22.  function Animal.prototype.setWeight(){  
  23.    this.weight = weight;      
  24.  }  
  25.  var animal = new Animal('Andy',13,13);  
  26.  document.write(animal.getAge());  
  27.  animal.setAname('Chenzw');  
  28.  document.write(animal.getAname());  
  29. </script>