跳到主要内容

语法

2023年06月27日
柏拉文
越努力,越幸运

一、constructor(x,y) public


class A {
a: string;
b: string;
constructor(a: string, b: string) {
this.a = a;
this.b = b;
}
}

const a = new A("哈哈", "嘻嘻");
console.log(a);

二、constructor(x,y) private


class A {
private a: string;
private b: string;
constructor(a: string, b: string) {
this.a = a;
this.b = b;
}
}

const a = new A("哈哈", "嘻嘻");
console.log(a);

三、constructor(public x, public y)


class B {
constructor(public a: string, public b: string) {}
}

const b = new B("哈哈", "嘻嘻");
console.log(b);

四、constructor(private x, private y)


class C {
constructor(private a: string, private b: string) {}
}

const c = new C("哈哈", "嘻嘻");
console.log(c);
console.log(c.a);