• 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)

    佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
  • 相关阅读:
    HTML5学习笔记-2.HTML基础
    HTML5学习笔记-1.网页入门
    Java学习笔记-8.多线程编程
    Java学习笔记-7.Java IO流
    Java学习笔记-6.Java异常处理
    Java学习笔记-5.常用数据结构
    Java学习笔记-4.继承与多态
    Java学习笔记-3.类与对象
    C语言学习笔记-10.文件
    .net core 中threadpool用法
  • 原文地址:https://www.cnblogs.com/taofx/p/4139429.html
Copyright © 2020-2023  润新知