What is object orientation?

The idea of object oriented is mainly object oriented, which abstracts a problem into a concrete object, and encapsulates the abstracted object and its attributes and methods into a class.

Object – oriented three elements encapsulation, inheritance, polymorphism

Class (class)

Defining classes: Essentially defining properties and methods

Class Person{name:string = 'Monkey King' // Class Person{name:string = 'Monkey King' // SayHello (){console.log(' instance method '); sayHello(){console.log(' instance method '); sayHello(' instance method '); } static sayBay(){console.log(' static method ')// Static sayBay(){console.log(' static method '); }} const per = new Person() cojnsole.log(per.name) // You need to create an instance to access console.log(person.age) // to access console.log(person.age) directly from the classCopy the code

Polymorphic what is a constructor? * * * * constructor ()

Constructors are used when a class needs to create multiple objects

class Dog{ name:string; age:number; // Constructor calls constructor(name:string,age:number){// In the instance method,this represents the current instance. // In the constructor, the current object is the newly created object This.name = name this.age = age} sayHello(){console.log(' woof woof ')}} const dog1 = new Dog(' woof woof ',2) Const dog2 = new Dog(' laifu ',3)Copy the code

Inheritance allows an object of one type to acquire properties and methods of an object of another type

class Animal{ name:string; age:number; Constructor (name:string,age:number){this.name = name this.age = age}} Class Dog extends Animal{// Make Dog extend Animal SayHello (){console.log(' console.log ')}} const dog = new dog (' console.log ') class Cat extends Animal{// SayHello (){console.log(' console.log ')}} const cat = new cat (' Mimi ',2) /** * Subclasses will have all the methods and attributes of their parent class * by inheritance, you can write common code from multiple classes in a single parent class, * so you just need to write a let all subclasses also have properties and methods of the parent * if you want to add some in subclasses not found in the parent class attribute or method directly in the line * if add in subclasses and parent the same method, the method in a subclass can override the parent class method, called rewriting method * /Copy the code

Super In a class method, super represents the parent of the current class

class Animal{ name:string; Constructor (constructor :string,age:number){this.name = name} sayHello(){console.log(' console.log ')}} class Dog extends Animal{ age:number; Constructor (name:string,age:number){constructor(name:string,age:number){constructor(name:string,age:number){constructor(name:string,age:number){constructor(name:string,age:number){ So we need to add super, super(name) this.age = age; } sayHello(){ super.sayHello(); // Super represents the parent of the current class, super represents Animal}}Copy the code

An abstract class

Abstract classes are not different from other classes except that they cannot be used to create objects. Abstract classes are classes that are designed to be inherited. Abstract methods can be added to abstract classes. Constructor (name:string,age:number){this.name = name} // Define an abstract method. Subclasses must override abstract methods. Abstract sayHello():void; } class Dog extends Animal{sayHello(){console.log(' woof woof ')}} const Dog = new Dog(' woof woof ')Copy the code

encapsulation

Class Person{//ts can add attribute modifiers in front of attributes /** * Public modified attributes can be accessed (modified) from any location default values * private private attributes can only be modified (accessed) within a class. */ Private name:string; */ Private name:string; */ Private name:string; age:number; constructor(name:string,age:number){ this.name = name; This.age = age} getName(){return this.name} getName(){console.log(' executed ')} set Name (value:string){this.name = value}} const per = new Person(' Ming ',12) console.log(per.name) call get name method b /** / / per.name = 'red '; / / per.name =' red '; / / per.name = 'red '; per.age = 13; console.log(per); // The property has been modifiedCopy the code