1. 类的成员属性:
(1) ts中的类,成员属性必须要声明后使用;
(2) ts中类的成员属性不是在构造函数中声明的,是在class内,方法外;
class Person {
username: string = ''; // 1. 此处 声明
constructor(name: string) {
this.username = name; // 2. 再 使用
}
}
2. 类的修饰符:
(1) public、protected、private、readonly;
(2) 通过修饰符可以对类中成员属性与成员方法进行访问控制;
public:公开的,所有的地方都能访问,属性和方法默认是public;
protected:受保护的,在类的内部和他的子类中才能访问;
private:私有的,只能在该对象(类)的内部才可以访问;
readonly:只读的,所有的地方都能读取,只有类的构造器中修改值;
/* Person 类 自身内部 */
class Person {
public pub_a: string = 'aaa'; // 公开
protected pro_b: string = 'bbb'; // 保护
private pri_c: string = 'ccc'; // 私有
readonly rea_d: string = 'ddd'; // 只读
constructor(name: string) {
console.log(this.pub_a); // ok
console.log(this.pro_b); // ok
console.log(this.pri_c); // ok
console.log(this.rea_d); // ok
this.rea_d = 'ddd_2'
}
say() {
console.log(this.pub_a); // ok
console.log(this.pro_b); // ok
console.log(this.pri_c); // ok
console.log(this.rea_d); // ok
this.rea_d = 'ddd_2'; // error
}
}
/* Person 类 的子类 */
class Student extends Person {
say() {
console.log(this.pub_a); // ok
console.log(this.pro_b); // ok
console.log(this.pri_c); // error
console.log(this.rea_d); // ok
this.rea_d = 'ddd_2'; // error
}
}
/* Person 类 的实例化 */
let p1 = new Person('kimoo');
console.log(p1.pub_a); // ok
console.log(p1.pro_b); // error
console.log(p1.pri_c); // error
console.log(p1.rea_d); // ok
p1.rea_d = 'ddd_2'; // error
3. 类的存取器:
(1)TS 支持 getters/setters 来截取对对象成员的访问;
class Person {
private _age: number = 1; // 私有
constructor(name: string) { }
get age(): number {
return this.age;
}
set age(age: number) {
if (age > 0 && age < 150) {
this._age = age;
}
}
}
/* Person 类 的实例化 */
let p1 = new Person('kimoo');
console.log(p1.age); // 以属性去访问
4. 类的静态属性与静态方法:
创建类的静态成员,这些属性、方法存在于类本身上面而不是类的实例上。
/*
* let p1 = new Person();
* let p2 = new Person();
* ....
* 通过 new 关键字,每次实例化一个类时,都会生成一个新的实例对象,这样会占
* 用更多的内存资源。可通过类的静态成员特性,避免这个问题,实现单例。
*
*/
class Mysql {
public static instance: any; // 'static' 声明类的静态属性
host: string;
port: number;
username: string;
password: string;
dbname: string;
constructor(host = '127.0.0.1', port = 3306, username = 'root', password = '', dbname = '') {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.dbname = dbname;
}
public static getInstance() { // 'static' 声明类的方法
if (!Mysql.instance) {
Mysql.instance = new Mysql();
}
return Mysql.instance; // 实现 单例
}
query() { }
insert() { }
update() { }
}
let db = Mysql.getInstance(); // 通过类的静态方法实例化
db.query();
db.insert();
5. 类的继承:
class Person {
private _a = 1;
constructor(public username: string, public age: number) {
this.username = username;
this.age = age;
}
getName() {
console.log(this.username);
}
public static say() {
console.log('hello')
}
}
/*
* 1. 子类 没有constructor时,会继承父类的constructor;
* 2. 父类的私有属性,不被继承;
* 3. 父类的静态属性、静态方法不被继承;
*/
class Student extends Person {
getAge() {
console.log(this.age);
}
}
let s1 = new Student('kimoo', 300);
s1.getAge();
s1.getName();
/*
* 4. 子类 具有constructor时,必须使用super继承父类的constructor;
*/
class Student2 extends Person {
// 在构造函数constructor的参数中如果直接使用public等修饰符,
// 则等同于同时创建了该属性
constructor(public username: string, public age: number, public type: string) {
super(username, age)
}
getType() {
console.log(this.type)
}
}
let s2 = new Student2('jack', 23, 'good');
s2.getName();
s2.getType();
6. 抽象类:
/*
* 1. 抽象类: 类似于将多个类的相同属性抽取出来形成一个类。
* 2. abstract 声明抽象类;
* 3. 抽象类不能被实例化;
* 4. 抽象类中的抽象方法没有具体代码,只做类型约束。
* 5. 如果一个类继承了抽象的父类,就必须实现所有抽象方法,否则这个子类还是必须得为抽象类。
*
*/
abstract class Person { // 1. 定义一个抽象类
username: string;
constructor(username: string) {
this.username = username;
}
say() {
console.log('hello')
}
abstract study(): void // 2. 抽象方法没有具体的代码
}
class Student1 extends Person { // 继承抽象类
study() {
console.log('study 001'); // 3. 子类中实现具体代码逻辑
}
}
class Student2 extends Person {
study() {
console.log('study 002'); // 4. 子类中实现具体代码逻辑
}
}
abstract class P extends Person { // 抽象子类
doWhat () {
console.log('do what')
}
}