본문 바로가기

WEB(Front-End)/Javascript

5. class 객체지향 ( 캡슐화, 상속, 다형성) getter,setter, public, private

 

클래스는 붕어빵을 만들 수 있는 틀

: 데이터는 들어있지 않고 틀만 정해놓는 것

선언 방법 (constructor, fields, methods)

class 클래스명{

        constructor(멤버변수1,멤버변수2){

                this.멤버변수1=멤버변수1;

                this.멤버변수2=멤버변수2;

         }

}

 

Object는 만들어 놓은 틀 클래스를 이용하여 새로운 인스턴스를 생성하면 Object가 된다. (메모리에 올라간다)

만들어진 클래스를 이용하여 객체 생성하는 방법

1. 변수선언 후 new 키워드이용하여 선언

const 변수명=new 클래스명(값1,값2);

 

 

 

 

 

 

Getter Setter

 

public private(변수 선언시 #변수명)

 

static

: static 오브젝트에 상관없이 클래스에 연결되어있다. 클래스 자체에 붙어있다.

 

상속

:extends, overriding 재정의 가능

 

다형성 (instanceOf) : 모든 클래스는 Object클래스를 상속받는다.

 

'use strict';
console.log("class 공부 시작!!");

// 클래스 : Template -> 클래스를 이용하여 실제 데이터를 넣어서 만든 것 Object
class Person{
    //constructor생성 중 field 값 name, age 선언
    constructor(name,age){
        this.name=name;
        this.age=age;
    }

    // methods
    speak(){
        console.log(`${this.name}: hello!!`);
    }

}
const hw=new Person('bGom',20);
console.log(hw);
hw.speak();


console.log("Setter Getter");
class User{
    constructor(firstName,lastName,age){
        this.firstName=firstName;
        this.lastName=lastName;
        this.age=age;
    }
    get age(){
        return this._age;
    }
    set age(value){
        // if(value<0){
        //     throw Error('age can not be negative');
        // }
        this._age=value<0?0:value;
    }
}
const user1=new User('Steve','Jobs',-1);
console.log(user1);
//사람의 나이가 -1이 되는것이 말이 되지 않는다.

console.log('Fields (public, private)');
class Experiment{
    publicField=2;
    #privateField=0;
}
const experiment=new Experiment();
console.log(experiment.publicField);
console.log(experiment.privateField); //undefined 클래스안에서만 접근 수정 가능

console.log('static!!');
// static 오브젝트에 상관없이 클래스에 연결되어있다. 클래스 자체에 붙어있다.
class Article{
    static publisher='Dream coding';
    constructor(articleNumber){
        this.articleNumber=articleNumber;
    }
    static printPublisher(){
        console.log(Article.publisher);
    }
}

const article1=new Article(1);
const article2=new Article(2);
console.log(article1.publisher); //undefined static은 class자체에 붙어있어서
console.log(Article.publisher);
Article.printPublisher();

console.log('상속 extends 하여 도형 넓이 구하기 구현');
class Shape{
    constructor(width,height,color){
        this.width=width;
        this.height=height;
        this.color=color;
    }
    draw(){
        console.log(`drawing ${this.color} color!`);
    }

    getArea(){
        return this.width*this.height;
    }
}
class Rectangle extends Shape{
    draw(){
        super.draw();
        console.log('이것은 직사각형');
    }
}
class Triangle extends Shape{
    getArea(){
        return super.getArea()/2;
    }
}

const rectangle=new Rectangle(20,20,'blue');
rectangle.draw();
console.log(rectangle.getArea());

const triangle=new Triangle(20,20,'red');
triangle.draw();
console.log(triangle.getArea());

console.log('instanceOf');
console.log(triangle instanceof Triangle);
console.log(triangle instanceof Rectangle);
console.log(rectangle instanceof Triangle);
console.log(rectangle instanceof Rectangle);
console.log(triangle instanceof Object);
console.log(rectangle instanceof Shape);