>  기사  >  웹 프론트엔드  >  JavaScript로 10가지 클래스 작성

JavaScript로 10가지 클래스 작성

王林
王林원래의
2023-05-12 15:36:38325검색

JavaScript는 다양한 유형의 프로그램과 애플리케이션을 작성하는 데 사용할 수 있는 인기 있는 프로그래밍 언어입니다. JavaScript에서 클래스는 객체를 생성하고 실제 개념을 시뮬레이션하는 데 사용됩니다. 이 기사에서는 각각 특정 목적과 기능을 가진 10가지 JavaScript 클래스를 소개합니다.

  1. Animal 클래스

Animal 클래스는 동물 개체를 만드는 데 사용되는 간단한 클래스입니다. 여기에는 이름, 나이, 성별 등과 같은 일부 속성이 포함되어 있으며 이름 가져오기, 성별 가져오기 등과 같은 일부 메서드를 제공합니다.

class Animal {
  constructor(name, age, gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  }
  
  getName() {
    return this.name;
  }
  
  getGender() {
    return this.gender;
  }
  
  getAge() {
    return this.age;
  }
}
  1. Person 클래스

Person 클래스는 사람 개체를 만드는 데 사용되는 더 복잡한 클래스입니다. 여기에는 이름, 나이, 직업과 같은 일부 속성이 포함되어 있으며 이름 가져오기, 직업 가져오기 등과 같은 일부 메서드를 제공합니다.

class Person {
  constructor(name, age, job) {
    this.name = name;
    this.age = age;
    this.job = job;
  }
  
  getName() {
    return this.name;
  }
  
  getJob() {
    return this.job;
  }
  
  getAge() {
    return this.age;
  }
}
  1. Vehicle 클래스

Vehicle 클래스는 차량 객체를 생성하는 데 사용되는 클래스입니다. 여기에는 자동차 모델, 번호판 번호, 엔진 유형과 같은 일부 속성이 포함되어 있으며 자동차 모델 가져오기, 번호판 번호 가져오기 등과 같은 몇 가지 방법을 제공합니다.

class Vehicle {
  constructor(model, plate, engineType) {
    this.model = model;
    this.plate = plate;
    this.engineType = engineType;
  }
  
  getModel() {
    return this.model;
  }
  
  getPlate() {
    return this.plate;
  }
  
  getEngineType() {
    return this.engineType;
  }
}
  1. Book class

Book 클래스는 책 개체를 만드는 데 사용되는 클래스입니다. 여기에는 책 제목, 저자, 출판 연도와 같은 일부 속성이 포함되어 있으며 책 제목 가져오기, 저자 가져오기 등과 같은 몇 가지 메서드를 제공합니다.

class Book {
  constructor(title, author, pubYear) {
    this.title = title;
    this.author = author;
    this.pubYear = pubYear;
  }
  
  getTitle() {
    return this.title;
  }
  
  getAuthor() {
    return this.author;
  }
  
  getPubYear() {
    return this.pubYear;
  }
}
  1. Food class

Food 클래스는 음식 객체를 만드는 데 사용되는 클래스입니다. 여기에는 이름, 카테고리 및 가격과 같은 일부 속성이 포함되어 있으며 이름 가져오기, 가격 가져오기 등과 같은 일부 메서드를 제공합니다.

class Food {
  constructor(name, category, price) {
    this.name = name;
    this.category = category;
    this.price = price;
  }
  
  getName() {
    return this.name;
  }
  
  getCategory() {
    return this.category;
  }
  
  getPrice() {
    return this.price;
  }
}
  1. Shape 클래스

Shape 클래스는 Shape 객체를 생성하는 데 사용되는 클래스입니다. 여기에는 유형, 크기, 색상과 같은 일부 속성이 포함되어 있으며 유형 가져오기, 면적 계산 등과 같은 일부 메서드를 제공합니다.

class Shape {
  constructor(type, size, color) {
    this.type = type;
    this.size = size;
    this.color = color;
  }
  
  getType() {
    return this.type;
  }
  
  getSize() {
    return this.size;
  }
  
  getColor() {
    return this.color;
  }
  
  calculateArea() {
    // code to calculate area
  }
}
  1. Movie 클래스

Movie 클래스는 영화 객체를 만드는 데 사용되는 클래스입니다. 여기에는 이름, 감독 및 출연과 같은 일부 속성이 포함되어 있으며 이름 가져오기, 주연 가져오기 등과 같은 일부 메서드를 제공합니다.

class Movie {
  constructor(title, director, actors) {
    this.title = title;
    this.director = director;
    this.actors = actors;
  }
  
  getTitle() {
    return this.title;
  }
  
  getDirector() {
    return this.director;
  }
  
  getActors() {
    return this.actors;
  }
}
  1. Employee 클래스

Employee 클래스는 Employee 객체를 생성하는 데 사용되는 클래스입니다. 여기에는 이름, 직업 번호 및 직위와 같은 일부 속성이 포함되어 있으며 이름 가져오기, 직위 가져오기 등과 같은 일부 메소드를 제공합니다.

class Employee {
  constructor(name, id, jobTitle) {
    this.name = name;
    this.id = id;
    this.jobTitle = jobTitle;
  }
  
  getName() {
    return this.name;
  }
  
  getId() {
    return this.id;
  }
  
  getJobTitle() {
    return this.jobTitle;
  }
}
  1. 컴퓨터 클래스

컴퓨터 클래스는 컴퓨터 객체를 생성하는 데 사용되는 클래스입니다. 여기에는 브랜드, 모델 및 운영 체제와 같은 일부 속성이 포함되어 있으며 브랜드 가져오기, 모델 가져오기 등과 같은 몇 가지 방법을 제공합니다.

class Computer {
  constructor(brand, model, os) {
    this.brand = brand;
    this.model = model;
    this.os = os;
  }
  
  getBrand() {
    return this.brand;
  }
  
  getModel() {
    return this.model;
  }
  
  getOS() {
    return this.os;
  }
}
  1. Article 클래스

Article 클래스는 기사 객체를 생성하는 데 사용되는 클래스입니다. 여기에는 제목, 작성자 및 콘텐츠와 같은 일부 속성이 포함되어 있으며 제목 가져오기, 콘텐츠 가져오기 등과 같은 일부 메서드를 제공합니다.

class Article {
  constructor(title, author, content) {
    this.title = title;
    this.author = author;
    this.content = content;
  }
  
  getTitle() {
    return this.title;
  }
  
  getAuthor() {
    return this.author;
  }
  
  getContent() {
    return this.content;
  }
}

위는 각각 특정 목적과 기능을 가진 10가지 JavaScript 클래스입니다. 이러한 클래스는 다양한 유형의 객체를 생성하는 데 사용할 수 있으며 코드 작성을 더욱 간결하고 효율적으로 만드는 데 편리한 여러 가지 방법을 제공합니다. 웹 페이지를 개발하든 애플리케이션을 개발하든 관계없이 JavaScript 클래스는 매우 유용한 도구이므로 일상적인 프로그래밍 작업에서 유연하게 사용해야 합니다.

위 내용은 JavaScript로 10가지 클래스 작성의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.