inheritance
Common inheritance
class Person {
name: string;
constructor(name: string) {
this.name = name
}
run(): string {
return `The ${this.name}In running `
}
getName(): string {
return this.name
}
setName(name: string) :void {
this.name = name
}
}
// Inherit the above method
class CoolPerson extends Person {
constructor(name: string) {
super(name)
}
work() {
console.log(`The ${this.name}In the work `)}}const p = new CoolPerson('coolFish')
console.log(p.getName())
Copy the code
polymorphism
Polymorphism: a parent class defines a method that is not implemented and lets its subclasses implement it. Each subclass behaves differently. Polymorphism is inheritance.
class Animal {
name: string
constructor(name: string) {
this.name = name;
}
eat() {
console.log('The way to eat')}}class Dog extends Animal {
constructor(name: string) {
super(name)
}
eat() {
console.log(`The ${this.name}Eating bones `)}}class Cat extends Animal {
constructor(name: string) {
super(name)
}
eat() {
console.log(`The ${this.name}Eat fish `)}}Copy the code
This defines an animal, and then both cats and dogs are descended from animals, but the way they define eating, they all behave differently.
An abstract class
Abstract classes in TS: They are base classes that provide inheritance from other classes and cannot be instantiated directly. The abstract keyword is used to define an abstract method of an abstract class that contains 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 standards. Standards: This class requires its subclasses to contain defined parameters and methods.
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(`The ${this.name}Meat `)}}class Cat extends Animal {
constructor(name: any) {
super(name)
}
play() {
console.log(`The ${this.name}Eat fish `)}}Copy the code
So we see that we’ve defined an abstract class for an animal, and there’s an eat method in there, and Dog inherits, Cat doesn’t, so Cat will report an error saying it can’t inherit because it didn’t implement eat.
interface
The role of interface: in object-oriented programming, interface is a specification definition, it defines the behavior and action of the specification, interface plays a restriction and specification, interface defines a batch of classes need to follow the specification.
The attribute interface
An attribute interface is a constraint on an incoming object
interface FullName {
firstName: string;
secondName: string;
}
function printName(name: FullName) {
console.log(name.firstName + name.secondName)
}
printName({ firstName: "123".threeName: "1231" }) // Error, does not conform to the interface definition
printName({ firstName: "123".secondName: "1231" })
let aug = {
let arg = {
age: 20.firstName: '123'.secondName: '123'
}
printName(arg) // No error is reported when passed as an object, as long as there is a match
Copy the code
In the above example, I define an attribute interface. There are two attributes, and the type is string, and the parameter type is this interface. Therefore, this parameter is also an object.
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 + The '-' + value
}
console.log(md5('name'.'coolFish'))
console.log(sha1('name'.'liSi'))
Copy the code
Defines the input and output parameters of a function.
Class type interfaces (mostly used)
Class type interfaces: Constraints on classes, similar to abstract classes. Methods in an abstract class can have a method body, which implements the specific functionality of the method, but not methods in an interface. Member variables in an abstract class can be of various types, whereas member variables in an interface can only be of public static final type. Interfaces cannot 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, while 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
Class type interface, which defines the properties and methods of a class.
The interface extension
Interface extension: An interface can inherit from an interface
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
Inheritance + implementation 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 games')}}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