mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 14:40:55 +08:00
add js
This commit is contained in:
parent
1469c2683c
commit
2211015ff5
30
examples/chapter02/js-class-example.js
Normal file
30
examples/chapter02/js-class-example.js
Normal file
@ -0,0 +1,30 @@
|
||||
// 父类
|
||||
class Animal {
|
||||
constructor(name) {
|
||||
this.name = name;
|
||||
}
|
||||
speak() {
|
||||
return "some animal sound";
|
||||
}
|
||||
}
|
||||
|
||||
// 子类
|
||||
class Dog extends Animal {
|
||||
speak() {
|
||||
return this.name +": Woof!";
|
||||
}
|
||||
}
|
||||
|
||||
// 子类
|
||||
class Cat extends Animal {
|
||||
speak() {
|
||||
return this.name +": Meow!";
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
let dog = new Dog("阿狗");
|
||||
let cat = new Cat("阿猫");
|
||||
|
||||
console.log(dog.speak()); // 输出: Woof!
|
||||
console.log(cat.speak()); // 输出: Meow!
|
Loading…
Reference in New Issue
Block a user