JS: object-oriented

Object-oriented:

It is to put the data and the operation method on the data together as an interdependent whole - the object. Abstract the commonality of similar objects to form a class. Most of the data in the class can only be processed by the methods of this class. A class has a relationship with the outside world through a simple external interface, and objects communicate with each other through messages. The program flow is determined by the user in use. An object is a concept that people abstract from various concrete objects. People come into contact with various objects every day, such as a mobile phone is an object.

Object-oriented programming (OOP: object-oriented programming)

The difference between object-oriented and process-oriented

project name object-oriented programming Process-oriented programming (also called structured programming)
definition Object-oriented, as its name implies, is to abstract the affairs in reality into "objects" in programming. Its basic idea is that everything is an object, and it is a "bottom-up" design language. First design components, and then complete assembly. Object-oriented, as its name implies, is to abstract the affairs in reality into "objects" in programming. Its basic idea is that everything is an object, and it is a "bottom-up" design language. First design components, and then complete assembly.
features encapsulation, inheritance, polymorphism Algorithm + Data Structure
Advantage Suitable for large and complex systems, easy to reuse, Suitable for simple systems, easy to understand
disadvantage More abstract, lower performance than process-oriented Difficult to deal with complex systems, difficult to reuse, difficult to maintain, difficult to expand
Compared Easy to maintain, easy to reuse, and easy to expand. Due to the characteristics of object-oriented encapsulation, inheritance, and polymorphism, a low-coupling system can be designed to make the system more flexible and easier to maintain  The performance is higher than that of object-oriented, because the class needs to be instantiated when calling, the overhead is relatively large, and it consumes more resources; such as single-chip microcomputer, embedded development, Linux/Unix, etc. generally adopt process-oriented development, and performance is the most important factor.
design language Java、Smalltalk、EIFFEL、C++、Objective-、C#、Python等 C、Fortran

1. Three main characteristics of object-oriented

encapsulation, inheritance, polymorphism

① Package

Two meanings:

One layer of meaning is to regard the attributes and behaviors of the object as an inseparable whole, and "encapsulate" the two in an inseparable independent unit (ie, the object); the other layer of meaning refers to "information hiding", and the different The information that needs to be known to the outside world is hidden. Some attributes and behaviors of objects allow outside users to know or use them, but they are not allowed to be changed. Other attributes or behaviors are not allowed to be known to the outside world, or only the functions of the object are allowed to be used. Possibly hides the functional implementation details of the object.

Advantages of Encapsulation

  • Good encapsulation can reduce coupling, which is in line with the pursuit of "high cohesion and low coupling" in program design.

  • The structure inside the class can be freely modified.

  • More fine-grained control over member variables is possible.

  • Hide message implementation details.

② Inheritance

Inheritance is a cornerstone of Java object-oriented programming techniques because it allows the creation of hierarchical hierarchies of classes.

Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance fields and methods of the parent class, or the subclass inherits the methods from the parent class, so that the subclass has the same behavior as the parent class.

 Benefits of inheritance:

(1) Improve the reusability of class code

(2) Improved code maintainability

(3) It makes the relationship between classes and classes, which is the premise of polymorphism (it is also a disadvantage of inheritance, and the coupling of classes is improved)

Inherited properties:

  • The subclass has the non-private properties and methods of the parent class.

  • Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.

  • A subclass can implement the method of the parent class in its own way, that is, override the method of the parent class.

  • Java inheritance is single inheritance, but multiple inheritance is possible. Single inheritance means that a subclass can only inherit one parent class. Multiple inheritance means, for example, class A inherits class B, and class B inherits class C, so according to the relationship, class C is B The parent class of the class, class B is the parent class of class A, which is a feature that distinguishes Java inheritance from C++ inheritance.

  • Inheritance can be achieved by using the keywords extends and implements, and all classes inherit from java.lang.Object. When a class does not have the two keywords of inheritance, it inherits object by default (this class is in java  . lang  package, so no need to  import ) ancestor class.

  • Improve the coupling between classes (the disadvantage of inheritance, the higher the coupling, the closer the connection between the codes and the worse the independence of the codes).

 function Animal(nick, age) {
        this.nick = nick;
        this.age = age;
    }
    Animal.prototype.skill = function() {
        console.log('觅食')
    }

    function Dog(nick, age, type) {
        // this.nick = nick;
        // this.age = age;
        Animal.call(this, nick, age);
        Animal.apply(this, [nick, age]);
        this.type = type;
    }
    // 我们使用call和apply方式实现构造函数的继承
    // call与apply作用:改变this指向

    // 浅拷贝,不能这么写,容易对父级的原型进行修改
    // Dog.prototype = Animal.prototype;
    // 推荐使用就for in
    for (var x in Animal.prototype) {
        Dog.prototype[x] = Animal.prototype[x]
    }

    Dog.prototype.jump = function() {
        console.log("jump");
    }

    var d = new Dog('dd', 6, '柯基');
    d.skill();

③ Polymorphism

Polymorphism is the ability to have multiple different manifestations or morphologies of the same behavior.

Advantages of polymorphism

  • 1. Eliminate the coupling relationship between types
  • 2. Substitution
  • 3. Scalability
  • 4. Interface
  • 5. Flexibility
  • 6. Simplicity

Three necessary conditions for polymorphism to exist

  • inherit
  • rewrite
  • Parent class references point to subclass objects

       For example: Parent p = new Child();

2. Constructor destructor prototype

Constructor: open up memory space

Destructor: release memory space

this: points to the instance object itself

Prototype: the public storage space of this class, used to save memory

    function Book(name, author, price, type) {
        // this指向实例对象本身
        this.name = name;
        this.author = author;
        this.price = price;
        this.type = type;

        // 为了避免内存浪费,不建议方法写在构造函数
        // this.sale = function() {
        //     console.log('全场8折');
        // }
    }
    // 建议:方法及公用属性写在原型中
    // 原型需要用构造函数名调用
    // 原型的本质是个对象
    Book.prototype.sale = function() {
        console.log('全场8折');
    }

    var shz = new Book('水浒传', '施耐庵', '56.6', '小说');
    var xyj = new Book('西游记', '吴承恩', '66.6', '小说');

3. Prototype chain

When accessing a certain method or property of an object, look it up in your own constructor, if not, look it up in your own prototype, if not, look up the prototype of the prototype, and always find the prototype of Object, a chain formed in this way is called prototype chain

    function Dog(type, name, age) {
        this.type = type;
        this.name = name;
        this.age = age;
    }
    Dog.prototype.skill = function() {
        console.log('拆家');
    }

    function Cat(kindle, nick) {
        this.tykindlepe = kindle;
        this.nick = nick;
    }
    Cat.prototype = new Dog('金毛', '疙瘩', 2);

    var cat = new Cat('橘猫', '啊桔');

    cat.skill();

reverse string

     正常写法  
     function reverseString(string) {
         return string.split('').reverse().join('');
     }


     面向对象写法
     String.prototype.reverseString = function() {
         return this.split('').reverse().join('');
     }

Guess you like

Origin blog.csdn.net/Cc200171/article/details/125104124