1. JS defines class class
//定义类
class Person {
constructor () {
}
run () {
}
}
2. TS defines the class class
// 定义class类
class Person{
name: string
age: number
constructor(name:string, age:number){
this.name = name
this.age = age
}
}
3. Class modifiers (public, private, and protected)
- Using the public modifier, the defined variables can be accessed internally and externally, if not written, the default is
public
/* public */
class Person {
public name: string
private age: number
protected some: any
constructor(name: string, age: number, some: any) {
this.name = name
this.age = age
this.some = some
}
run() {
}
}
let tom = new Person('Tom', 18, '青青子衿')
tom.name
- Using the private modifier, the defined variable is private and can only be accessed internally, not externally
/* private */
class Person {
public name: string
private age: number
protected some: any
constructor(name: string, age:number, some: any) {
this.name = name
this.age = age
this.some = some
}
run() {
}
}
let tom = new Person('Tom', 18, '青青子衿')
tom.age
tom.some
- Using the protected modifier means that the defined variable is private and can only be accessed internally and in inherited subclasses, not externally
/* protected */
class Person {
public name: string
private age: number
protected some: any
constructor(name: string, age: number, some: any) {
this.name = name
this.age = age
this.some = some
}
run() {
}
}
class Man extends Person {
constructor() {
super('张三', 18, 1)
console.log(this.some);
}
created() {
console.log(this.some);
}
}
let tom = new Person('Tom', 18, '青青子衿')
let man = new Man
man.some
4. static static properties and static methods
- The properties and functions defined by static cannot be accessed through this, but can only be called through the class name
/* static静态属性和静态方法 */
class Person {
public name: string
private age: number
protected some: any
static nb: string = '123'
constructor(name: string, age: number, some: any) {
this.name = name
this.age = age
this.some = some
// this.nb //static定义的属性,不可以通过this去访问,只能通过类名去调用
// this.run() //static定义的函数,不可以通过this去访问,只能通过类名去调用
}
static run() {
return console.log(this.name);
}
}
Person.nb
- Note: If both functions are static, they can call each other through this
class Person {
public name: string
private age: number
protected some: any
static nb: string = '123'
constructor(name: string, age: number, some: any) {
this.name = name
this.age = age
this.some = some
}
static run() {
return this.aaa()
}
static aaa(){
return '青青子衿,悠悠我心'
}
}
Person.nb
5. interface definition class
- Interface defines the class, using the keyword implements, followed by multiple names of the interface separated by commas, or using extends for inheritance
interface PersonClass {
get(type: boolean): boolean
}
interface PersonClass2{
set():void,
asd:string
}
class A {
name: string
constructor() {
this.name = "123"
}
}
class Person extends A implements PersonClass,PersonClass2 {
asd: string
constructor() {
super()
this.asd = '123'
}
get(type:boolean) {
return type
}
set () {
}
}
6. Abstract class
- Abstract class defined by abstract
- The methods defined by abstract can only be described and cannot be implemented
- abstract class cannot be instantiated
abstract class A {
name: string
constructor(name: string) {
this.name = name;
}
print(): string {
return this.name
}
abstract getName(): string
}
class B extends A {
constructor() {
super('小满')
}
getName(): string {
return this.name
}
}
let b = new B();
console.log(b.getName());