es 6クラスタイプのtypescriptの中の類、文章の3
クラスを定義
class Person {
name: string; // public , public
constructor(name: string) {
// ,
this.name = name
}
run(): void {
console.log(this.name + ' ')
}
getNane():string {
return this.name
}
setName(name: string):void {
this.name = name
}
}
//
var p = new Person(' ')
p.run() //
p.getNane() //
p.setName(' ') // name =
引き継ぐclass Person {
name: string // public , public
constructor(name: string) {
// ,
this.name = name
}
run(): void {
console.log(this.name + ' ')
}
}
// extends , super
class Student extends Person {
constructor(name: string) {
super(name) //
}
// ,
work(): void {
console.log(this.name + ' ')
}
// , , , ,
run(): void {
console.log(this.name + ' ')
}
}
var s = new Student(' ')
s.run() //
s.work() //
クラスの中の修饰符// ,typescript
/**
* public : 、 、
* protected : 、 。
* private : 。 、
* , public
*/
// public
class Person {
public name: string
constructor(name: string) {
this.name = name
}
run(): void {
console.log(this.name)
}
}
class Student extends Person {
constructor(name: string) {
super(name)
}
log(): void {
console.log(this.name)
}
}
let p = new Person(' ')
console.log(p.name) //
let s = new Student(' ')
s.log() //
// protected
class Person {
protected name: string
constructor(name: string) {
this.name = name
}
run(): void {
console.log(this.name)
}
}
class Student extends Person {
constructor(name: string) {
super(name)
}
log(): void {
console.log(this.name)
}
}
let p = new Person(' ')
// protected , js, ts
console.log(~~p.name~~ ) //
let s = new Student(' ')
s.log() //
// private
class Person {
private name: string
constructor(name: string) {
this.name = name
}
run(): void {
console.log(this.name)
}
}
class Student extends Person {
constructor(name: string) {
super(name)
}
log(): void {
// ,
console.log(~~this.name~~ )
}
}
let p = new Person(' ')
p.run() // name
// ,
console.log(~~p.name~~ ) //
let s = new Student(' ')
s.log() //
クラスの中の静的な属性と方法/**
* 、
* static
*/
class Person {
name: string
static code: number = 1
constructor(name: string) {
this.name = name
}
run(): void {
console.log(this.name)
}
static print(): void {
//
console.log(' ' + ~~this.name~~ ) //
//
console.log(' ' + Person.code) // 1
console.log(' ')
}
}
console.log(Person.code) // 1
Person.print() //
多形:多形は継承に属する// : , , ,
class Animal {
name: string
constructor(name: string) {
this.name = name
}
// , eat
// , eat
eat(food: string): void {
console.log(`${this.name} ${food}`)
}
}
class Dog extends Animal {
constructor(name: string) {
super(name)
}
}
//
let dog = new Dog(' ')
dog.eat(' ') //
class Cat extends Animal {
constructor(name: string) {
super(name)
}
// ,
eat(food: string): void {
console.log(`${this.name} ${food}, `)
}
}
let cat = new Cat(' ')
cat.eat(' ') // ,
typescriptの抽象類/**
* typescript , ,
* abstract ,
*
*/
abstract class Animal {
name: string
constructor(name: string) {
this.name = name
}
//
abstract eat(): void
}
// ,,
// let animal = new Animal(' ')
// abstract , ,
class Dog extends Animal {
constructor(name: string) {
super(name)
}
// ,
eat(): void {
console.log(this.name + ' ')
}
}
let dog = new Dog(' ')
dog.eat() //