八:静态成员、静态类、枚举、重载和覆盖
最后更新于:2022-04-01 20:32:07
## 一、创建静态成员
直接将属性或者方法赋给类本身
~~~
function Person(name,age)
{
this.myname = name;
this.age = age;
}
Person.showInfo = function() //静态方法,只能访问静态属性
{
return ("我的性别是:"+Person.sex);
};
var per = new Person("yuan",20);
Person.sex = "男"; //静态属性
alert(Person.showInfo());
~~~
Google中运行结果:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec559bca.jpg)
也可以改为this.sex访问,结果相同。但是不可以用this.myname/this,age访问实例属性。
## 二 、注意事项
2.1 静态成员一般是公共成员,不能被继承,但可以在子类中直接使用
2.2 JS中允许定义同名的静态属性和实例属性,二者互相独立
~~~
function Person(name,age,sex)
{
this.myname = name;
this.age = age;
this.sex = sex;
}
Person.showInfo = function() //静态方法,只能访问静态属性
{
return ("我的性别是:"+Person.sex);
};
var per = new Person("yuan",20,"女");
Person.sex = "男"; //静态属性
alert(Person.showInfo());
~~~
运行结果:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec559bca.jpg)
2.3 静态方法中不能访问实例属性,且不能使用关键字super,但是实例方法可以访问静态属性和实例属性
~~~
function Person(name,age,sex)
{
this.myname = name;
this.age = age;
this.sex = sex;
}
Person.prototype.showInfo = function() //实例方法
{
return ("我的性别是:"+Person.sex+"\n我的年龄是:"+this.age);
};
var per = new Person("yuan",20,"女");
Person.sex = "男"; //静态属性
alert(per.showInfo());
~~~
运行结果:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec579179.jpg)
2.4 如果一个类的成员全是静态成员,则这个类是静态类,不能被实例化。
## 三 、枚举:一组静态常量
~~~
function Day()
{
throw new Error("这是静态类,不能实例化");
}
Day.MONDAY = 1;
Day.TUE = 2;
Day.WED = 3;
var date = new Date();
var day = date.getDay();
switch(day)
{
case Day.MONDAY:
alert("周一");
break;
case Day.TUE:
alert("周二");
break;
case Day.WED:
alert("周三");
break;
}
~~~
## 四、重载和覆盖
4.1 重载:在JS中,不允许有两个同名的方法,且参数没有数据类型,所以JS中的重载是通过参数的个数不同实现的
~~~
function OverLoad()
{
this.method = function()
{
var len = arguments.length;
if(len == 2)
{
document.write("正在调用的是两个参数的方法
"); } else if(len == 3) { document.write("正在调用的是三个参数的方法"); } } } try { var over = new OverLoad; over.method(1,2); over.method(1,2,3); } catch(err) { document.write(err); } ~~~ 运行结果 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec58e580.jpg) 4.2 覆盖:用于继承中,指子类中定义了与父类中同名的方法,父类的同名方法被隐藏 ~~~ //父类 function Father(myName,age) { this.myname = myName; this.age = age; } Father.prototype.show = function() { return("Father's name: "+this.myName+", and age: "+this.age); }; //子类 function Child(myName,age) { this.$super = Father; //继承父类 this.$super(myName,age); //调用父类的构造方法 this.show = function() { return ( "Child's age: "+this.age); }; } Child.prototype = new Father(); var child = new Child("yuan",20); alert(child.show()); ~~~ 运行结果: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec5a2d20.jpg)
';
"); } else if(len == 3) { document.write("正在调用的是三个参数的方法"); } } } try { var over = new OverLoad; over.method(1,2); over.method(1,2,3); } catch(err) { document.write(err); } ~~~ 运行结果 ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec58e580.jpg) 4.2 覆盖:用于继承中,指子类中定义了与父类中同名的方法,父类的同名方法被隐藏 ~~~ //父类 function Father(myName,age) { this.myname = myName; this.age = age; } Father.prototype.show = function() { return("Father's name: "+this.myName+", and age: "+this.age); }; //子类 function Child(myName,age) { this.$super = Father; //继承父类 this.$super(myName,age); //调用父类的构造方法 this.show = function() { return ( "Child's age: "+this.age); }; } Child.prototype = new Father(); var child = new Child("yuan",20); alert(child.show()); ~~~ 运行结果: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-08-30_57c54ec5a2d20.jpg)