Typescript object-oriented class and interface permissions readonly and private
Class: defines the abstract characteristics of everything.
Object: an instance of a class
Three characteristics of object-oriented:
encapsulation: hide internal implementation, only provide public interface to the outside.
Inheritance: subclass inherits the parent class and has the properties and methods of the parent class.
Polymorphism: when rewriting or implementing related properties and methods, different subclasses can There are different implementations
Property access rights
private: Restrict the properties to be accessed only in this class, and neither subclasses nor instances can be accessed.
Protected: Restrict the use of this class and subclasses, and the instance cannot be used. Can be understood as heritage
public: unlimited
const、readonly 和 private
const is used to define a variable whose memory address cannot be modified.
readonly acts on the variable, indicating that the attribute cannot be modified. The
private scope attribute indicates whether the attribute can be accessed.
Classes and interfaces
A class can only have one parent class, but it can implement multiple interfaces. The
interfaces can also inherit from each other.
Except for optional attributes that are not allowed to be implemented, the class must implement all the attribute methods in the interface in terms of quantity and type
// 接口提取相同的特性
interface A {
A(): void;
}
interface B {
B(): string;
}
interface C extends A, B {
}
class ClassA implements C {
A() {
}
B(): string {
return ''
}
}