This is the sixth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

inheritance

Common inheritance

class Person { name: string; Constructor (name: string) {this.name = name} run(): string {return '${this.name}} getName(): string { return this.name } setName(name: string): Void {this.name = name}} class CoolPerson extends Person {constructor(name: constructor) String) {super(name)} work() {console.log(' ${this.name} working ')}} const p = new CoolPerson('coolFish') console.log(p.getName())Copy the code

polymorphism

Polymorphism: a parent class defines a method that is not implemented but is implemented by a subclass that inherits it. Each subclass behaves differently. Polymorphism is inheritance.

class Animal { name: string constructor(name: string) { this.name = name; }} class Dog extends Animal {constructor(name:) {constructor() {console.log(' constructor ')}} class Dog extends Animal {constructor(name:)} String) {super(name)} eat() {console.log(' ${this.name} eat bone ')}} class Cat extends Animal {constructor(name: constructor) String) {super(name)} eat() {console.log(' ${this.name} eat fish ')}}Copy the code

This defines an animal, and then both cats and dogs inherit from the animal, but the defined way to eat, behave differently.

An abstract class

Abstract classes in TS: They are base classes that provide inheritance from other classes and cannot be instantiated directly. Abstract methods of an abstract class are defined using the abstract keyword. Abstract methods in an abstract class contain no concrete implementation and must be implemented in a derived class. Abstract methods can only be placed in abstract classes. Abstract classes and methods are used to define the standard: this class requires its subclasses to contain the parameters and methods defined.

abstract class Animal { public name: string constructor(name: string) { this.name = name } abstract eat(): any } class Dog extends Animal { constructor(name: Any) {super(name)} eat() {console.log(' ${this.name} eat ')}} class Cat extends Animal {constructor(name: constructor) Any) {super (name)} play () {the console. The log (` ${this. The name} fish `)}}Copy the code

So what we see is that we’ve defined an abstract class for an animal, which has an eat method, which Dog inherits, which Cat doesn’t, so Cat is going to report an error saying it can’t inherit because it didn’t implement the eat method.

interface

The role of an interface: In object-oriented programming, an interface is a definition of a specification that defines behavior and actions. An interface acts as a limitation and a specification. An interface defines the specification that a class needs to follow.

The attribute interface

Property interfaces are constraints passed in to objects

interface FullName { firstName: string; secondName: string; } function printName(name: FullName) { console.log(name.firstName + name.secondName) } printName({ firstName: PrintName ({firstName: "123", secondName: "1231"}) printName({firstName: "123", secondName: "1231") "1231"}) let Aug = {let arg = {age: 20, firstName: '123', secondName: '123'} printName(argCopy the code

In the example above, I defined a property interface with two properties of type string, and the parameter type is this interface, so this parameter is also an object, and the internal properties and types must be one to one, if passed in as an object, then the object has the corresponding properties.

Function type interface

interface encrypt {
  (key: string, value: string): string;
}
var md5: encrypt = function (key: string, value: string): string {
  return key + value
}
var sha1: encrypt = function (key: string, value: string): string {
  return key + '---' + value
}
console.log(md5('name', 'coolFish'))
console.log(sha1('name', 'liSi'))
Copy the code

Defines the incoming and outgoing parameters of the function.

Class type interface (used more)

Class type interface: a constraint on a class, similar to an abstract class. Methods in an abstract class can have a method body, that is, implement the specific function of the method, but methods in an interface cannot. Member variables in an abstract class can be of any type, while member variables in an interface can only be of public static final type. Interfaces cannot also have static code blocks and static methods (methods decorated with static), whereas abstract classes can have static code blocks and static methods. A class can inherit only one abstract class, but a class can implement multiple interfaces.

interface Animal { name: string; eat(str: string): void; } class Dog implements Animal { name: string constructor(name: String) {this.name = name} eat() {console.log(this.name + 'meat ')} play() {console.log('123')}}Copy the code

The class type interface defines the properties and methods of the class.

The interface extension

Interface extension: Interfaces can inherit interfaces

interface Animal {
  eat(): string
}
interface Person extends Animal {
  work(): void
}
class Web implements Person {
  public name: string
  constructor(name: string) {
    this.name = name
  }
  eat() {
    return '123'
  }
  work() {
    console.log(321)
  }
}
Copy the code

Inherit + implement the interface

interface Animal { eat(): string } interface Person extends Animal { work(): void } class CoolFish { public name: string constructor(name: String) {this.name = name} play() {console.log(' play ')}} Class Web extends CoolFish implements Person {public name:  string constructor(name: string) { super(name) this.name = name } eat() { return '123' } work() { console.log(321) } } let w = new Web('coolFish') w.play()Copy the code

\