aardio实现汽车类和人类类
本帖最后由 axuanup 于 2024-2-7 23:45 编辑import console
//实现汽车类
class Car {
ctor(make, model, color, year) {
this.make = make
this.model = model
this.color = color
this.year = year
}
startEngine = function() {
return '汽车引擎启动...'
}
stopEngine = function() {
return '汽车引擎关闭...'
}
drive = function() {
return this.make + this.model + '正在行驶...'
}
displayDetails = function() {
return '汽车详细信息: ' + '制造商: ' + this.make + ', 型号: ' + this.model + ', 颜色: ' + this.color + ', 年份: ' + this.year
}
}
var myCar = Car('奥迪', 'A4', '黑色', 2020)
console.log(myCar.startEngine())
console.log(myCar.drive())
console.log(myCar.stopEngine())
console.log(myCar.displayDetails())
//实现人类类
class Human{
ctor(name, age, gender) { //类构造函数初始化人类的姓名,年龄和性别
this.name = name
this.age = age
this.gender = gender
}
eat = function() { //一个吃饭的方法
..console.log(this.name + "正在吃饭。")
}
sleep = function() { //一个睡觉的方法
..console.log(this.name + "正在睡觉。")
}
work = function() { //一个工作的方法
..console.log(this.name + "正在工作。")
}
displayHuman = function() { //一个显示人类信息的方法
..console.log("姓名: " + this.name + ",年龄:" + this.age + ",性别:" + this.gender)
}
}
newHuman = Human("张三", 25, "男")
newHuman.eat()
newHuman.sleep()
newHuman.work()
newHuman.displayHuman()
console.pause(true)
页:
[1]