l-hammer / l-hammer/learning

TS 类(classes)

Open
#3 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

typescript
Dominant language
No language data
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

什么是类?

ECMAScript 2015规范中,引入了class的概念,让我们能够使用基于类的面向对象的模式。TypeScript中,我们可以直接使用这些特性。

先来看一个简单示例:

class Greeter {
  greeting: string;

  constructor(message: string) {
    this.greeting = message;
  }

  greet() {
    return `Hello ${this.greeting}`;
  }
}

const greeter = new Greeter("world");

示例中我们声明了一个Greeter类,这个类包含3个成员,一个greeting属性,一个构造函数和一个greet方法。最后我们通过new关键字构造了Greeter类的一个实例。

继承

TypeScript中,我们可以使用常用的面向对象模式。基于类的程序设计中一种最基本的模式就是允许使用继承来扩展现有的类。

示例:

class Animal {
  move(distance: number = 0) {
    console.log(`Animal moved ${distance}m.`);
  }
}

class Dog extends Animal {
  bark() {
    console.log("Woof! Woof!");
  }
}

const dog = new Dog();

dog.bark();
dog.move(10);

示例展示了最基本的继承。Dog是一个派生类(子类),通过extends关键字派生至Animal基类(超类)。

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue contains Chinese documentation about TypeScript classes and inheritance, but it does not name a documentation file or specify a requested change. First inspect the repository's documentation structure and locate the appropriate TypeScript learning page. Completion criteria are not defined in the issue, so the intended edit should be clarified before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.