본문 바로가기

Web/Typescript

class

class = 실제 세계의 물체를 표한하는데 사용

정보(변수)와 행동(함수)


인터페이스와 차이점


  • constructor
  • 함수의 내용이 꼭 있어야한다


class

class Point {
    x: number;
    
    constructor(x: number, public y: number = 0) {
        this.x = x;
    }

    dist() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }

    static origin = new Point(0, 0);
}

var point1 = new Point(10, 20);
var point2 = new Point(25);


Interface

interface Point {
    x: number;
    y: number;

    dist(): number;
}

var origin: Point = {
    x: 0, y: 0,
    dist: () => Math.sqrt(this.x * this.x + this.y * this.y)
};
var point1: Point = {
    x: 10, y: 20,
    dist: () => Math.sqrt(this.x * this.x + this.y * this.y)
};
var point2: Point = {
    x: 25, y: 0,
    dist: () => Math.sqrt(this.x * this.x + this.y * this.y)
};


'Web > Typescript' 카테고리의 다른 글

callback, popup  (0) 2017.03.22
Interface  (0) 2017.03.22
오브젝트 (Object)  (0) 2017.03.22
반복문 (Loops)  (0) 2017.03.21
조건문 (conditions)  (0) 2017.03.21