class Rectangle{
constructor(height,width){
this.height=height;
this.width=width;
this.area=this.height * this.width
}
get getAreaInGet(){
return '面积为'+this.height * this.width+'--get得出'
}
getArea(){
return '面积为'+this.height * this.width+'--getArea方法得出,非get得出'
}
}
// extends 创建子类
class Animal{
constructor(name){
this.name=name
}
speak(){
console.log(this.name+'动物发出叫声')
}
}
class Dog extends Animal{
speak(){
console.log(this.name+'狗叫')
}
}
// 使用super 调用超类
class Cat{
constructor(name){
this.name=name
}
speak(){
console.log(this.name+'猫科类动物发出的叫声')
}
}
class Lion extends Cat{
speak(){
super.speak() // 调用父对象上的函数
console.log(this.name+'狮子的叫声')
}
}
class Monkey{
constructor(age,sex){
this.age='Monkey里的age'+age;
this.sex='Monkey里的sex'+sex;
this.name='猿猴'
}
}
class People extends Monkey{
constructor(age,sex){
super(age,sex)
this.name='人类'
}
}
参考
1、https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes
2、https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/class
3、http://es6.ruanyifeng.com/#docs/class
网友评论