定义
class A {
constructor(name,age) {
this.name = name;
this.age = age
}
}
TypeScript是不允许直接在constructor 定义变量的,需要在constructor上面先声明
class A {
name: string;
age: string
constructor(name:string, age:string) {
this.name = name;
this.age = age
}
}
如果定义了变量,但是不用也会报错
可以设置默认值消除这个错误
class A {
name: string;
age: string = '1'
constructor(name: string, age: string) {
this.name = name;
}
}
类的修饰符
公共,私有与受保护的修饰符 public private protected
class A {
public name: string;
private age: number;
protected sex: string
constructor(name: string, age: number, sex: string) {
this.name = name;
this.age=age;
this.sex=sex
}
}
使用public修饰符,可以让你定义的变量可以内部访问,也可以外部访问,如果不写默认就是public
class A {
public name: string;
private age: number;
protected sex: string
constructor(name: string, age: number, sex: string) {
this.name = name;
this.age=age;
this.sex=sex
}
}
let zl=new A('zl',18,'man');
console.log(zl.name);
使用 private修饰符,代表定义的变量私有的只能在内部访问,不能在外部访问
使用protected修饰符,代表定义的变量,私有的只能在内部和继承的子类中访问,不能在外部访问
class A {
public name: string;
private age: number;
protected sex: string
constructor(name: string, age: number, sex: string) {
this.name = name;
this.age=age;
this.sex=sex
}
}
class B extends A{
constructor(){
super('zl',18,'man');
console.log(this.sex);
}
f(){
console.log(this.sex);
}
}
let zl=new A('zl',18,'man');
console.log(zl.sex);
static 静态属性和静态方法
用static定义的属性,不可以通过this去访问,只能通过类名去调用
正确调用
class A {
public name: string;
private age: number;
protected sex: string;
static specialization: string
constructor(name: string, age: number, sex: string, specialization: string) {
this.name = name;
this.age = age;
this.sex = sex
A.specialization = specialization
}
}
用static定义的函数,不可以通过this去访问,也只能通过类名去调用
正确调用
class A {
public name: string;
private age: number;
protected sex: string;
static specialization: string
constructor(name: string, age: number, sex: string, specialization: string) {
this.name = name;
this.age = age;
this.sex = sex;
A.specialization = specialization;
A.f()
}
static f() {
console.log('f');
}
}
需注意: 如果两个函数都是static 静态的,是可以通过this互相调用
class A {
public name: string;
private age: number;
protected sex: string;
static specialization: string
constructor(name: string, age: number, sex: string, specialization: string) {
this.name = name;
this.age = age;
this.sex = sex;
A.specialization = specialization;
A.f()
}
static f() {
this.fn();
console.log('f');
}
static fn() {
console.log('fn');
}
}
interface定义类
ts中,使用关键字implements实现接口,实现多个接口,接口间用逗号隔开,继承还是用extends
interface B {
get(type: boolean): boolean
}
interface C {
set(): void;
asd: string
}
class A {
name: string;
constructor(name: string) {
this.name = name;
}
}
class Person extends A implements B, C {
asd: string;
constructor(name: string) { // 添加name作为Person的构造函数参数
super(name); // 传递name给父类A的构造函数
this.asd = 'zl';
}
set(): void {
}
get(type: boolean): boolean {
return type;
}
}
5.抽象类
应用场景如果你写的类实例化之后毫无用处此时我可以把它定义为抽象类
或者你也可以把他作为一个基类-> 通过继承一个派生类去实现基类的一些方法
例1,报错,抽象类无法实例化
abstract class A {
}
new A()
例2
我们在A类定义了 getName 抽象方法,但未实现
我们B类实现了A定义的抽象方法,如不实现就不报错,我们定义的抽象方法必须在派生类实现
abstract class A {
name: string;
constructor(name: string) {
this.name = name;
}
print(): string {
return this.name
}
abstract getName(): string
}
class B extends A {
getName(): string {
return this.name
}
}