In JS and TS there is a definition of a class. A class describes the common properties and methods of the object being created.
1. The main module in the class
Fields: Fields are variables declared in a class. Fields represent data about an object.
Constructor: called when a class is instantiated to allocate memory for its objects.
Methods: Methods are the operations to be performed by the object.
The following is the JS conversion of a module in class TS
class Car {
/ / field
engine:string;
// constructor
constructor(engine:string) {
this.engine = engine
}
/ / method
disp():void {
console.log("Engine is:"+this.engine)
}
}
Copy the code
var Car = / * *@class * / (function () {
// constructor
function Car(engine) {
this.engine = engine;
}
/ / method
Car.prototype.disp = function () {
console.log("Engine is:" + this.engine);
};
returnCar; } ());Copy the code
2. Use of classes
We need to use the new keyword to instantiate the class and use it as an example:
class Car {
/ / field
engine:string;
// constructor
constructor(engine:string) {
this.engine = engine
}
/ / method
disp():void {
console.log("Engine type displayed in function:"+this.engine)
}
}
// Create an object
var obj = new Car("XXSY1")
// Access the field
console.log("Read engine model:"+obj.engine)
// Access methods
obj.disp()
Copy the code
There are exceptions, however, where static methods can be called directly from a class. We need to use the static keyword to declare static methods and properties.
class StaticMem {
static num:number;
static disp():void {
console.log("Num value is"+ StaticMem.num)
}
}
StaticMem.num = 12 // Initialize static variables
StaticMem.disp() // Call static methods
Copy the code
3. Inheritance in classes
Class inheritance is implemented using the extends keyword, which inherits all properties and methods from the original class.
class Shape {
Area:number
constructor(a:number) {
this.Area = a
}
}
class Circle extends Shape {
disp():void {
console.log("Area of a circle:"+this.Area)
}
}
var obj = new Circle(223);
obj.disp() // The area of the circle is 223
Copy the code
4. Access modifiers in classes
There are three types of pulic for access modifiers in a class: public properties or methods, all of which can access protected: properties or methods, only themselves and their subclasses and superclasses can access private: private properties or methods, which can only be accessed by the class in which they are defined.
class Encapsulate {
str1:string = "hello" // No modifier represents public
private str2:string = "world"
protected str3: number = 23
_myAge(): string { // Methods with private attributes are accessible
return 'hello' + this.str2
}
}
var obj = new Encapsulate()
console.log(obj.str1) / / accessible
console.log(obj.str2) Str2 is private
console.log(obj.str3) // error compiling, str2 is protected
class Child extends Encapsulate {
constructor() {
super(a)// The inherited must run super()
console.log(this.str2) Str2 is private
console.log(this.str3) / / accessible
console.log(this._myAge()) / / accessible}}Copy the code
5. Interfaces and classes
Classes can inherit from an interface using implements, but they must contain properties and methods from the interface
interface ILoan {
interest:number
}
class AgriLoan implements ILoan {
interest:number
rebate:number
constructor(interest:number,rebate:number) {
this.interest = interest
this.rebate = rebate
}
}
var obj = new AgriLoan(10.1)
console.log("Profit is:"+obj.interest+", draw into:"+obj.rebate )
Copy the code
6. An abstract class
Abstract methods 1. Abstract methods can only be declared, but not implemented, and cannot be instantiated. They can only be referenced by declarations and cannot create objects.
2. If a class has abstract methods, the class must be abstract, but the abstract class need not have abstract methods, abstract classes can have constructors;
3. A subclass inherits an abstract class. If the subclass does not wish to be an abstract class, it must implement all the abstract methods declared in the parent class.
4. The abstract method only preserves the function of the method, and the concrete implementation is handed over to the subclass that inherits the abstract class, and the subclass overrides the abstract method.
abstract class Animal {
eat(food: string) :void {
console.log(food)
}
abstract run(distance: number) :void
}
class Dog extends Animal {
run(distance: number) :void {
console.log(distance)
}
}
const d = new Dog()
d.run(100)
d.eat('asdf')
Copy the code