class People{
constructor(name,age){//类似java的构造方法,不过只能有一个
this.name = name
this.age = age
}
get height1(){//类似java的属性的get方法
console.log("get")
return this.height
}
set height1(newHeight){//类似java的属性的set方法
console.log("set")
this.height = newHeight
}}
let p = new People("liyang",26)
console.log(p.name)
console.log(p.age)
p.height1 = 170
console.log(p.height1)
输出:
liyang
26
set
get
170
继承
class Student extends People{
constructor(name,age,weight){//还是只有一个构造方法
super(name,age)//必须调用父类的构造方法
this.weight = weight
}
say(){
super.say()//写与不写都会默认调用父类的,(测试过),和java的小差别
return "方法重写:" + this.weight
}}
let s = new Student("li","26","120")
console.log(s.say())
静态属性和方法
class Student extends People{
static hangle(){
console.log("静态方法")
}}
Student.a = 10//静态属性
console.log(Student.a)//静态属性的使用
Student.hangle()//静态方法的调用