Three main features of object-oriented programming —— encapsulation, inheritance, polymorphism

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 attributes
     }
 A.prototype.aa=function(){// One of the methods above the prototype function is the specific prototype writing method
        console.log(this.a)
     }
 A.prototype.aaa=new C()
   
 function B(){/ / class
         this.b="b"
         A.call(this);// Borrow the constructor to inherit the attributes of a
    }
 //B.prototype=A.prototype;
   B.prototype=new A();
   B.prototype.bb=function(){// One of the methods above the prototype function is the specific prototype writing method
        console.log(this.b)
     }

     function C(){/ / class
         this.c="c"
         A.call(this);
     }
    C.prototype=new A();// Note: must be placed first
    C.prototype.cc=function(){
        console.log(this.c)
     }
     var a =new A();/ / instantiate
     var b =new B();/ / instantiate
     var C =new C();/ / instantiate
    b.bb();// call the method above the prototype through A.A.A
    console.log(a)
    console.log(C)

Copy the code
<script>
    / / parent class 01
    class Person {
        constructor(name) {
            this.name = name;
        }
        showName() {
            console.log("I'm the showname in the superclass.")
            return 'Name is:The ${this.name}`}}/ / subclass
    class Student extends Person {
        constructor(name, skill) {
            super(name);// Execute the parent constructor again
            this.skill = skill;// Attributes of the subclass
        }
        showName(){
            super.showName();// If you write a function with the same name as the parent class, the method in the parent class will be changed
            // Override, if you do not want to override super.showname () can be called so that the parent and subclass execute together
            console.log("I'm the showname in the subclass.")}showSkill() {
            return The skills of the students are:The ${this.skill}`}}let stu1 = new Student("strive"."Play truant")
    console.log(stu1.showSkill());
    console.log("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --")
    console.log(stu1.showName());

</script>
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