"use strict"; // 属性的封装:令属性更加安全 (function () { /** * readonly 只读 * static * public 共有的 * private 私有的 只能在当前类中访问及修改 * 若想要更改 则需在类中定义方法 * protected 受保护的 只能在当前累及当前类的子类中访问及使用 */ class Person { constructor(name1, age1) { this.age1 = age1; this.name1 = name1; } /** * 在属性的封装中 * getter 获取属性值 get * setter 设置属性值 set */ get name() { return this.name1; } set name(value) { this.name1 = value; } get age() { return this.age1; } set age(value) { if (value > 0) { this.age1 = value; } } } let p = new Person("孙悟空", 20); // p.name = "猪八戒"; // p.age = 10; // console.log(p.getName()); // console.log(p.setName("哈哈")); // console.log(p.setAge(-10)) p.name = '小小'; p.age = 1; console.log(p); console.log(p.name); // console.log(Person.age) // class A { // public name: string; // public age: number; // constructor(name: string, age: number) { // this.name = name; // this.age = age; // } // } // class A { // constructor(public name:string,public age:number) { // this.name = name // this.age = age // } // } class B { constructor(num) { this.num = num; } } class C extends B { say() { console.log(this.num); } } let c = new C(100); console.log(c); // C.num = 1; })();