First of all, what is object orientation?
- Object Oriented is a software development method. Object-oriented concepts and applications have gone beyond the program design and software development, such as database system, interactive interface, application structure, application platform, distributed system, network management structure, CAD technology, artificial intelligence and other fields.
- The object – oriented approach is to objectify things, including their properties and behavior. Object-oriented programming is closer to the idea of real life. Generally speaking, the bottom of object-oriented is still process oriented, process abstract into classes, and then encapsulated, convenient use is object oriented (everything is object).
Object – oriented has three main characteristics: encapsulation, inheritance, polymorphism
1. The encapsulation
For encapsulation, an object encapsulates its own properties and methods, so it does not need to rely on other objects to complete its own operations. Encapsulation also has many benefits, such as: reduced coupling rate, repeated invocation of attributes in the class to improve security
2. Inheritance (the most important part of object-oriented programming)
Inheritance means that a child object can inherit the attributes and behaviors of the parent object, that is, the parent object owns the attributes and behaviors, and the child object also owns these attributes and behaviors. This is very similar to the inheritance of species in nature.
-
A.call(this); Disadvantages: Inherits only member attributes, not attributes on the prototype
-
2.2. Original type inheritance B.prototype=A.prototype; Disadvantages: This method inherits only attributes on the stereotype, not member attributes
-
2.3. Instantiate A class b.protoType =new A(); Disadvantages: Inherited member attributes on the prototype may not be detectable bugs
-
2.4. Combinatorial inheritance (secure inheritance) A.call(this); + B.prototype=new A(); Disadvantages: The parent class executes twice
-
2.5. Parasitic inheritance
-
2.6 Combined Parasitic Inheritance (best)
Function A(){// class this. A =” A “// member attribute} a.prototype. aa=function(){// prototype function console.log(this.a)} A.prototype.aaa=new C()
Function B(){this.b=” B “a.all (this); } // b.protoType = a.prototype; B.prototype=new A(); Prototype =function(){console.log(this.b)}
Function C(){this.c=" C "a.all (this); } C.prototype=new A(); C =function(){console.log(this.c)} var a =new a (); Var b =new b (); Var C =new C(); // instantiate b.b (); // Call the prototype method console.log(a) console.log(C) through A.A.ACopy the code
Copy the code
Copy the code
3. The polymorphism
Polymorphism refers to that the same operation and instances of different classes will have different execution results, that is, when objects of different classes receive the same message, they will get different results. The three necessary conditions for implementing polymorphism are: 1. Inheritance 2. Overwrite 3