首页 > 代码库 > javascript面向对象实例

javascript面向对象实例

非私有属性

function Student(name, gender, age, grade, teacher){     

          this.name = name;    

          this.gender = gender;    

          this.age = age;      

          this.grade = grade;     

          this.teacher = teacher;

}    

var bob = new Student("bob", "male", 15, 10, "Marlow");   

    alert(bob.age); //输出15  

var susan = new Student("susan", "female", 10, 5, "Gresham"); 

    alert(susan.gender); //输出 ‘female‘

var bob = new Student("bob", "male", 15, 10, "Marlow");  

  alert(bob.age); //输出15     bob.age = 9; 

  alert(bob.age); //输出9;

#####################################################################################################################################

私有属性

function Student(name, gender, age, grade, teacher)     {     

   var studentName = name;    

   var studentGender = gender;   

   var studentGrade = grade;    

   var studentTeacher = teacher;   

   var studentAge = age;    

       this.getAge = function() {       

                return studentAge;  

       };        

       this.setAge = function(val){     

                  studentAge = Math.abs(val); //使用绝对值,确保年龄是正值     

       }; 

}    

       var bob = new Student("bob", "male", 15, 10, "Marlow");   

       alert(bob.studentAge); //未定义,因为年龄在类定义中受私有保护   

       alert(bob.getAge()); //输出15     bob.setAge(-20);     alert(bob.getAge()); //输出20

#####################################################################################################################################

动态创建的访问器方法

function Student( properties ){   

         var $this = this;  //将类范围存储到名为$this的变量中    

             //迭代处理对象的属性     

                for ( var i in properties ) {       

                         (function(i) {       

                                      // 动态创建访问器方法     

                                     $this[ "get" + i ] = function(){               

                                             return properties[i];        

                                                                       };      

                                     })(i);     

                }  

}   

                 // 创建一个新的用户对象实例,并传递属性的对象  

                   var student = new Student({Name: "Bob",Age: 15,Gender: "male"});   

                    alert(student.name); //因属性是私有的而未定义  

                    alert(student.getName()); //输出 "Bob"  

                    alert(student.getAge()); //输出15   

                    alert(student.getGender()); //输出 "male"

#####################################################################################################################################

类的继承

function Worker() {   

     this.getMethods = function(properties, scope)

     {         var $this = scope;  //将类范围存储到名为$this的变量中    

                 //迭代处理对象的属性       

                  for ( var i in properties ){       

                          (function(i){                 // 动态创建访问器方法          

                                    $this[ "get" + i ] = function()          

                                          {

                                              return properties[i];

                                              };             //动态地创建一个分析整数,并确保是正值的更改器方法。        

                                     $this[ "set" + i ] = function(val) {

                                        

                                                 if(isNaN(val)) {

                                                    

                                                      properties[i] = val; 

                                                                   

                                              }else{      

                                                              

                                                   properties[i] = Math.abs(val);

                                                  

                                              }

                                      };

                          })(i);        

                }    

     };

}

// CommissionWorker "子类"和WageWorker "子类"

//继承Worker的属性和方法。

CommissionWorker.prototype = new Worker();

WageWorker.prototype = new Worker();

 

function CommissionWorker(properties) {    

      this.getMethods(properties, this);    

      //计算收入    

      this.getIncome = function(){       

           return properties.Sales * properties.Commission;

      }

}

//要求有下列属性:薪水、每周小时数、每年周数

function WageWorker(properties) {    

    this.getMethods(properties, this);  

      //计算收入    

    this.getIncome = function() {       

         return properties.Wage * properties.HoursPerWeek * properties.WeeksPerYear;   

          }

}

var worker = new WageWorker(Name: "Bob", Wage: 10, HoursPerWeek: 40,WeeksPerYear: 48 });

alert(worker.wage); //未定义。薪水是私有属性。

worker.setWage(20); alert(worker.getName());   //输出 "Bob"

alert(worker.getIncome()); //输出 38,400 (20*40*48)

var worker2 = new CommissionWorker( {     Name: "Sue",     Commission: .2,     Sales: 40000 });

alert(worker2.getName());   //输出 "Sue"

alert(worker2.getIncome()); //输出8000(2% 乘40,000)

 

javascript面向对象实例