Typescript(七)中的类
类的定义 继承,封装和多态三要素
类基础
关键字class - 构造函数constructor - 实例化
简易入门版
class Point {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
getPositon() {
return `${this.x} + ${this.y}`;
}
}
const point = new Point(1, 2);
console.log(point.getPositon());
修饰符
public
- public 表示 公共的 可以在任何地方访问
class Point {
public x: number;
public y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
public getPosition() {
return `(${this.x}, ${this.y})`;
}
}
private
- private 私有的 不能被继承,不能被实例,全部受限制
- 只能在class里面使用
class Parent {
private age: number;
constructor(age: number) {
this.age = age;
}
}
const p = new Parent(18);
console.log(p);
// { age: 18 }
console.log(p.age);
// error 属性“age”为私有属性,只能在类“Parent”中访问
console.log(Parent.age);
// error 类型“typeof ParentA”上不存在属性“age”
class Child extends Parent {
constructor(age: number) {
super(age);
console.log(super.age); // 通过 "super" 关键字只能访问基类的公共方法和受保护方法
}
}
protected
- protected 受保护的 可以被继承,不能被实例,部分受限制
class Parent {
protected age: number;
constructor(age: number) {
this.age = age;
}
protected getAge() {
return this.age;
}
}
const p = new Parent(18);
console.log(p.age);
// error 属性“age”为私有属性,只能在类“ParentA”中访问
console.log(Parent.age);
// error 类型“typeof ParentA”上不存在属性“age”
class Child extends Parent {
constructor(age: number) {
super(age);
console.log(super.age); // undefined
console.log(super.getAge());
}
}
new Child(18);
readonly 只读的
- readonly 只读的 只能在构造函数里面赋值,不能被修改
class UserInfo {
readonly name: string;
constructor(name: string) {
this.name = name;
}
}
const user = new UserInfo("Lison");
user.name = "haha"; // error Cannot assign to 'name' because it is a read-only property
static 静态的
- static 静态的 可以在类里面直接调用,不能在实例里面调用
class Parent {
public static age: number = 18;
public static getAge() {
return Parent.age;
}
constructor() {
//
}
}
const p = new Parent();
console.log(p.age);
// error Property 'age' is a static member of type 'Parent'
console.log(Parent.age);
// 18
可选类属性
- 一个?号表示可有可无的属性
class Info {
name: string;
age?: number;
constructor(
name: string,
age?: number,
public gender?: string,
) {
this.name = name;
this.age = age;
}
}
const info1 = new Info("lison");
const info2 = new Info("lison", 18);
const info3 = new Info("lison", 18, "man");
存储器(把私有的属性暴露出去)
set和get方便把私有的属性暴露出去
class Parent {
private _name: string;
constructor(name: string) {
this._name = name;
}
set name(name: string) {
this._name = name;
}
get name() {
return this._name;
}
}
const PersonOne = new Parent("哈哈哈");
console.log(PersonOne.name);
抽象类
抽象类只能被继承,不能被实例化,类里面也不能被调用
abstract class People {
constructor(public name: string) {}
abstract printName(): void;
}
class Man extends People {
constructor(name: string) {
super(name);
this.name = name;
}
printName() {
console.log(this.name);
}
}
const m = new Man(); // error 应有 1 个参数,但获得 0 个
const man = new Man("lison");
man.printName(); // 'lison'
const p = new People("lison"); // error 无法创建抽象类的实例
类和类之间的关系
- 类实现接口通过 implements 关键字
interface FoodInterface {
type: string;
}
class FoodClass implements FoodInterface {
// error Property 'type' is missing in type 'FoodClass' but required in type 'FoodInterface'
static type: string;
constructor() {}
}
- 类继承类
class Parent {
constructor(public name: string) {
this.name = name;
}
}
class Man extends Parent {
constructor(
public name: string,
public sex: string,
) {
super(name);
this.sex = sex;
}
}
const ManPerson = new Man("今天", "男");
console.log(ManPerson.sex);
评论区