这两天有点忙,一直都没有时间,现在开始写一下JS的类
为什么要提到类? 因为面向对象的设计,类是必须的
为什么要用面向对象的方式设计程序? 因为面向对象的设计是简单的,尤其是对一个我们无法控制全部的大项目而言,面向过程就显得力不从心了。
废话不说了, 正式开始
类这个话题现在已经说的很多了,它就像是一个模板,每个对象都通过这个模板产生出来,亦称为实例。就如我们这么多人抽象一下叫人类,而人类的实例有几十亿个。
英文版本:
<script type="text/javascript">
/*
1、建立类
每种语言建立类的方法都不同,Javascript建立类用function,先看下面一个类的定义:
*/
function BbsUser()
{
// private variable
var intAge=27;
// 这里一定要用this来构建函数副本,只有这样才能构建多个闭包。
// method
this.SetAge=function(a){intAge=a;};
this.GetAge=function(){return intAge};
// class property
BbsUser.ArticleNumber=0;
// prototype property
BbsUser.prototype.ArticleNumber=0;
// prototype method
BbsUser.prototype.CreateArticle=function()
{
window.document.write(this.Name+"在论坛刚发了一帖<br/>");
this.ArticleNumber++;
BbsUser.prototype.ArticleNumber++;
}
BbsUser.prototype.DeleteArticle=function()
{
window.document.write(this.Name+"在论坛刚删了一帖<br/>");
this.ArticleNumber--;
BbsUser.prototype.ArticleNumber--;
}
// property
this.Name="";
this.ArticleNumber=0;
}
/*上面的代码定义了一个BbsUser(论坛用户)类,它有个属性Name(名字) 。
一个类有固定的属性,但类的实例却有不同的属性值。这个很好理解,就像每个人的名字不同一样。
2、声明一个类的实例
*/
var will=new BbsUser(); // 实例1:我
var fish=new BbsUser(); // 实例2:小鱼
/*
3、类的属性
will和fish都是BbsUser这个类的实例,它们都有Name属性,因此,我们可以这样设置它的属性
*/
will.Name="willpower";
window.document.write(will.Name+"<br/>");
fish.Name="小鱼";
window.document.write(fish.Name+"<br/>");
/*同样输出了小鱼,这说明不同实例的Name属性值是不同的。
当然属性也可以有默认值的,论坛每天都会有很多新的帖子,统计帖数的属性ArticleNumber
*/
will.CreateArticle();
fish.CreateArticle();
window.document.write(will.Name+"的年龄为:"+will.GetAge()+"<br/>");
window.document.write(fish.Name+"的年龄为:"+fish.GetAge()+"<br/>");
will.SetAge(20);
window.document.write(will.Name+"更改后的年龄为:"+will.GetAge()+"<br/>");
window.document.write(will.Name+"在论坛里的发帖数为:"+will.ArticleNumber+"<br/>");
window.document.write(fish.Name+"在论坛里的发帖数为:"+fish.ArticleNumber+"<br/>");
window.document.write("本论坛所有的帖子数为:"+BbsUser.prototype.ArticleNumber+"<br/>");
will.DeleteArticle();
window.document.write(will.Name+"在论坛里的发帖数为:"+will.ArticleNumber+"<br/>");
window.document.write(fish.Name+"在论坛里的发帖数为:"+fish.ArticleNumber+"<br/>");
window.document.write("本论坛所有的帖子数为:"+BbsUser.prototype.ArticleNumber+"<br/>");
</script>
中文版本:
<script type="text/javascript">
/*
1、建立类
每种语言建立类的方法都不同,Javascript建立类用function,先看下面一个类的定义:
*/
function 论坛成员()
{
// private variable
var 年龄=27;
// 这里一定要用this来构建函数副本,只有这样才能构建多个闭包。
// method
this.设置年龄=function(a){年龄=a;};
this.得到年龄=function(){return 年龄};
// class property
论坛成员.发帖数=0;
// prototype property
论坛成员.prototype.发帖数=0;
// prototype method
论坛成员.prototype.发帖=function()
{
window.document.write(this.姓名+"在论坛刚发了一帖<br/>");
this.发帖数++;
论坛成员.prototype.发帖数++;
}
论坛成员.prototype.删帖=function()
{
window.document.write(this.姓名+"在论坛刚删了一帖<br/>");
this.发帖数--;
论坛成员.prototype.发帖数--;
}
// property
this.姓名="";
this.发帖数=0;
}
/*上面的代码定义了一个BbsUser(论坛用户)类,它有个属性Name(名字) 。
一个类有固定的属性,但类的实例却有不同的属性值。这个很好理解,就像每个人的名字不同一样。
2、声明一个类的实例
*/
var will=new 论坛成员(); // 实例1:我
var fish=new 论坛成员(); // 实例2:小鱼
/*
3、类的属性
will和fish都是BbsUser这个类的实例,它们都有Name属性,因此,我们可以这样设置它的属性
*/
will.姓名="willpower";
window.document.write(will.姓名+"<br/>");
fish.姓名="小鱼";
window.document.write(fish.姓名+"<br/>");
/*同样输出了小鱼,这说明不同实例的姓名属性值是不同的。
当然属性也可以有默认值的,论坛每天都会有很多新的帖子,统计帖数的属性发帖数
*/
will.发帖();
fish.发帖();
window.document.write(will.姓名+"的年龄为:"+will.得到年龄()+"<br/>");
window.document.write(fish.姓名+"的年龄为:"+fish.得到年龄()+"<br/>");
will.设置年龄(20);
window.document.write(will.姓名+"更改后的年龄为:"+will.得到年龄()+"<br/>");
window.document.write(will.姓名+"在论坛里的发帖数为:"+will.发帖数+"<br/>");
window.document.write(fish.姓名+"在论坛里的发帖数为:"+fish.发帖数+"<br/>");
window.document.write("本论坛所有的帖子数为:"+论坛成员.prototype.发帖数+"<br/>");
will.删帖();
window.document.write(will.姓名+"在论坛里的发帖数为:"+will.发帖数+"<br/>");
window.document.write(fish.姓名+"在论坛里的发帖数为:"+fish.发帖数+"<br/>");
window.document.write("本论坛所有的帖子数为:"+论坛成员.prototype.发帖数+"<br/>");
</script>

最新评论