這篇文章主要介紹“JavaScript怎么寫十個不同的類”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“JavaScript怎么寫十個不同的類”文章能幫助大家解決問題。
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;
}
}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;
}
}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;
}
}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;
}
}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;
}
}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
}
}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;
}
}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;
}
}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;
}
}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怎么寫十個不同的類”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。