js object-oriented and object-oriented three characteristics

A: js object-oriented programming

In es5 so we are going to write object-oriented programming approach:

the Person function (name) {
      // constructor methods and properties inside 
    the this ._name = name;
     the this .getName = function () { 
         the console.log ( the this ._name); 
     }; 
     the this .setName = function (name) {
          the this = ._name name; 
     }; 
 } 
 
 the let P = new new the Person ( "John Doe " ); 
 p.getName ();   // Zhang 
 p.setName ( "John Doe " ); 
 p.getName ();   // John Doe

In es6, provides a new way to write object-oriented programming, this approach is closer to the object-oriented way of writing, but in itself is only a syntactic sugar:

// definition of class 
class
the Person { // constructor of the class, instantiation are performed when new constructor (name) { the this ._name = name; } getName () { the console.log ( the this ._name); } the setName (name) { the this ._name = name } } the let P = new new preson ( 'John Doe') p.getName ();
// Zhang p.setName ( 'John Doe ' ); p.getName (); / / John Doe

 II: three characteristics of object-oriented

Are three characteristics: encapsulation, polymorphism and inheritance

1: a package

Methods and classes we usually are used in an encapsulation, when we project development method, function code is encountered when some reusable many places, we can he packaged into a single function, so we need place a direct call to use it.

2: Inheritance

Inheritance is mainly used in our project development for the subclass inherits the parent class, the following is es6 inherited method of writing

 
 
class Father {
   constructor (name) {
          the this ._name = name;
      } 
   // instance method, the instance of the object call
getName () { the console.log ( the this ._name); }
   // static methods are not inherited, and by the class name to call
   static hitXiaoMing () {
         the console.log ( "play Bob" )
      }
}
  
 Class Son {Father the extends
      constructor (name, Age) {
          // handle class instance data when passed to the parent class subclasses (super herein must have, super parameters within the parent is inherited by instantiating required data) 
         Super (name);
          the this ._age = Age;
      } }
  
 var Daming = new new Father ( 'Ming ' );
Father.HitXiaoMing (); // hitting Hsiao Ming 
DaMing.GetName (); // Daming

 var XiaoMing = new Son('小明',15);
 
 
 XiaoMing.getName(); //小明
 

Special note : instance attributes and methods inherited instance inherits the parent class, not inherited static properties and static methods and static methods can only be invoked through the class name to.

Three: Polymorphism

 Polymorphism manifested method overloading and method overrides:

Method overloading: overload refers to different functions using the same function name, but different numbers of parameters or the type of the function. When the call to distinguish between different functions according to the parameters of the function

Method override: rewritable (also called coverage) means re base class virtual function (note the virtual function) re-implemented in a derived class. That is the function name and parameters are the same, just not the same realization Functions

Let's according to the above examples illustrate the method to add a bit of work to rewrite method:

class Father { 
   constructor (name) { 
         the this ._name = name; 
     } 
   // instance method, the instance of the object call 
     getName () { 
         the console.log ( the this ._name); 
     } 
Work () {
  the console.log ( 'my work is working so hard to make money to support their families')
}    
// static methods are not inherited, and by the class name to call    static hitXiaoMing () { console.log ( " beat Xiao Ming " ) } } class Son Father {the extends constructor (name, Age) { // when the instance handle subclasses of class data to the parent class (super herein must have, super parameters are data in the parent class inherited by instantiating required) Super (name ); the this._age = age; }
     Work () { 
  console.log ( 'My job is to learn, every day.')
}
} 
 
 Var Daming = new new Father ( ' Ming ' ); 
 DaMing.work () // my job is working so hard to make money to support their families.
 var xiaoming = new new Son ( ' Bob ' , 15 ); 
XiaoMing.work (); // My job is to learn every day.

Above we rewrite the work () method of the parent class.

 

Three features of advantages:

Package: a package has the advantage of definitions can only operate within a class attributes, these attributes can not find fault outside, in order to modify, by only your encapsulation method defined above;

Inheritance: Inheritance reduce code redundancy, a number of repeated codes is omitted, the developer can define all subclasses have some attributes and methods from parent classes bottom, to achieve the purpose of coupling;

Polymorphism: polymorphism to achieve a personalized method, different subclasses can be implemented in different ways according to specific conditions, the light has a parent class definition method is not flexible enough to meet the special situation of the stretched

Guess you like

Origin www.cnblogs.com/zhengyufeng/p/10992972.html