[源码]JavaScript面向对象编程实践
(C)ShiShengSoft原创文章,转载请注明出处:http://blog.csdn.net/shishengsoft/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0019)http://auto.qq.com/ -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JavaScript面向对象编程实践</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
-->
</style></head>
<body>
<script src="jsTimer.js" language="javascript"></script>
<script language="javascript">
// ************************************************************************************
// 类的封装:属性、方法、事件
// ************************************************************************************
// 雄鹰
// ************************************************************************************
function Tercel(sName,iMaxAge){
this.Age = 0;
this.MaxAge = iMaxAge;
this.Name = sName;
this.pullulate = pullulate;
this.Dead = function(){};
}
function pullulate(){
Print("雄鹰在成长...<br>");
while (this.Age<=this.MaxAge){
Print((this.Age++) + "<br>");
}
this.Dead();
}
// ************************************************************************************
// 野兔
// ************************************************************************************
function Hare(sName,iMaxAge){
this.MaxAge = iMaxAge;
this.Name = sName;
}
Hare.prototype.Age = 0;
Hare.prototype.pullulate = function(){
Print("野兔在成长...<br>");
while (this.Age<=this.MaxAge){
Print((this.Age++) + "<br>");
}
this.Dead();
}
Hare.prototype.Dead = function(){}
// ************************************************************************************
// 鲨鱼
// ************************************************************************************
function Shark(sName,iMaxAge){
this.Age = 0;
this.MaxAge = iMaxAge;
this.Name = sName;
this.pullulate = function(){
Print("鲨鱼在成长...<br>");
while (this.Age<=this.MaxAge){
Print((this.Age++) + "<br>");
}
this.Dead();
}
this.Dead = function(){};
}
// ************************************************************************************
// 类的继承
// ************************************************************************************
// 动物〔基类〕
// ************************************************************************************
function Animal(sName){
var m_iAge, m_iMaxAge;
var m_DeadFunc, m_pullulateFunc;
this.Age = 0;
this.MaxAge = 0;
this.Name = sName;
this.Create = function(){
m_iAge = this.Age;
m_iMaxAge = this.MaxAge;
m_DeadFunc = this.Dead;
m_pullulateFunc = this.pullulate;
MyTimer.setEnable(true);
Print(this.Name + "诞生.<br>");
}
this.Dead = function(){};
this.pullulate = function(){};
var MyTimer = new jsTimer(100);
MyTimer.TimerEvent = function(){
if ((++m_iAge) > m_iMaxAge){
MyTimer.setEnable(false);
m_DeadFunc(); //死亡事件
}
else{
m_pullulateFunc(m_iAge);//成长事件
}
}
}
// ************************************************************************************
// 鲸鱼
// ************************************************************************************
function Whale(sName,iMaxAge){
this.Inherit = Animal;
this.Inherit();
delete this.Inherit;
//Animal.call(this);
this.Name = sName;
this.MaxAge = iMaxAge;
this.Swim = function(){
Print(this.Name + "在海里游动.<br>");
}
}
// ************************************************************************************
// 天鹅
// ************************************************************************************
function Swan(sName,iMaxAge){
//Animal.call(this);
this.Name = sName;
this.MaxAge = iMaxAge;
this.Fly = function(){
Print(this.Name + "在天空飞翔.<br>");
}
}
Swan.prototype = new Animal();
// ************************************************************************************
// 多重继承
// ************************************************************************************
function Roc(sName,iMaxAge){
Animal.call(this);
Whale.call(this);
Swan.call(this);
this.Name = sName;
this.MaxAge = iMaxAge;
}
// ************************************************************************************
</script>
<p>类的封装</p>
<p>
<input name="btnTercel" type="button" id="btnTercel" value="雄鹰" />
40
<input name="btnHare" type="button" id="btnHare" value="野兔" />
8
<input name="btnShark" type="button" id="btnShark" value="鲨鱼" />
70</p>
<p>继承和多态</p>
<p>
<input name="btnAnimal" type="button" id="btnAnimal" value="动物">
<input name="btnWhale" type="button" id="btnWhale" value="鲸鱼" />
<input name="btnSwan" type="button" id="btnSwan" value="天鹅" />
</p>
<p>多重继承 </p>
<p>鲸鱼+天鹅=鲲鹏
<input name="btnRoc" type="button" id="btnRoc" value="鲲鹏" />
</p>
<div id="Pad" style="overflow-y:scroll;font-size:12px; padding:10px 10px 10px; width:320px; height:240px; border:2px;
display:block; color:#FFFFFF; background-color:#000000"> </div>
<script type="text/javascript" language="javascript">
// ************************************************************************************
function Print(sText){
Pad.innerHTML+=sText;
}
function Clear(){
Pad.innerHTML="";
}
// ************************************************************************************
function btnTercel::onclick(){
Clear();
var MyTercel = new Tercel("雄鹰",40);
MyTercel.Dead = function(){
Print(MyTercel.Name + "生命结束.<br>");
}
MyTercel.pullulate();
}
function btnHare::onclick(){
Clear();
var MyHare = new Hare("野兔",8);
MyHare.Dead = function (){
Print(MyHare.Name + "生命结束.<br>");
}
MyHare.pullulate();
}
function btnShark::onclick(){
Clear();
var MyShark = new Shark("鲨鱼",70);
MyShark.Dead = SharkDead;
MyShark.pullulate();
}
function SharkDead(){
Print("Shark生命结束.<br>");
}
// ************************************************************************************
function btnAnimal::OnClick(){
Clear();
var MyAnimal = new Animal("动物");
MyAnimal.Dead = function(){
Print(MyAnimal.Name + "生命结束.<br>");
}
MyAnimal.pullulate = function(){
Print("活动.<br>");
}
MyAnimal.Create();
}
function btnWhale::onclick(){
Clear();
var MyWhale = new Whale("鲸鱼",10);
MyWhale.Dead = function(){
Print(MyWhale.Name + "生命结束.<br>");
}
MyWhale.pullulate = function(){
MyWhale.Swim();
}
MyWhale.Create();
}
function btnSwan::onclick(){
Clear();
var MySwan = new Swan("天鹅",10);
MySwan.Dead = function(){
Print(MySwan.Name + "生命结束.<br>");
}
MySwan.pullulate = function(){
MySwan.Fly();
}
MySwan.Create();
}
// ************************************************************************************
var iRnd;
function btnRoc::onclick(){
Clear();
var MyRoc = new Roc("鲲鹏",10);
MyRoc.Dead = function(){
Print(MyRoc.Name + "生命结束.<br>");
}
MyRoc.pullulate = function(){
if (iRnd=!iRnd)
MyRoc.Fly()
else
MyRoc.Swim();
}
MyRoc.Create();
}
</script>
</body>
</html>