首頁  >  文章  >  web前端  >  JavaScript寫十個不同的類別

JavaScript寫十個不同的類別

王林
王林原創
2023-05-12 15:36:38325瀏覽

JavaScript是一門流行的程式語言,它可以用來編寫各種類型的程式和應用程式。在JavaScript中,類別被用來創造物件和模擬實現真實世界的概念。在這篇文章中,我將介紹十個不同的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類別

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類別

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類別是用來建立形狀物件的類別。它包含一些屬性,如類型、尺寸和顏色,並提供一些方法,例如取得類型、計算面積等。

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類別是用來建立員工物件的類別。它包含一些屬性,如姓名、工號和職位,並提供一些方法,例如取得姓名、取得職位等。

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. Computer類別

Computer類別是用來建立電腦物件的類別。它包含一些屬性,如品牌、型號和作業系統,並提供一些方法,例如取得品牌、取得型號等。

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;
  }
}

以上是十個不同的JavaScript類,每個類別都有其特定的用途和功能。這些類別可用於建立各種類型的對象,並提供了許多方便的方法,使得程式碼編寫更為簡潔、高效。無論是開發網頁還是開發應用程序,JavaScript類別都是非常有用的工具,在日常的程式設計工作中應該靈活地使用它們。

以上是JavaScript寫十個不同的類別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn