Atlas使用接口

本文介绍了一个使用JavaScript实现的面向对象编程示例,包括抽象类、接口、继承等概念的应用,并展示了如何通过具体类来实现抽象类及接口的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 
<head>
  
<title>interface.html</title>
  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  
<meta http-equiv="description" content="this is my page">
  
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
  
<script type="text/javascript" src="Atlas.js"></script>
 
</head>
 
<script type="text/javascript">
    Type.registerNamespace(
"Demo.Animals");
Demo.Animals.Ipet 
= function(){
 
this.getFriendly = Function.abstractMethod();
}

Demo.Animals.Ipet.registerInterface(
'Demo.Animals.Ipet');
Demo.Animals.Animal 
= function(name){
 
var _name = name;
 
this.getName = function(){
  
return _name;
 }

}

Demo.Animals.Animal.registerAbstractClass(
'Demo.Animals.Animal');
Demo.Animals.Animal.prototype.toStringCustom 
= function(){
 
return this.getName();
}

Demo.Animals.Animal.prototype.speak
=Function.abstractMethod;
Demo.Animals.Pet
=function(name,friendlyName){
 Demo.Animals.Pet.registerAbstractClass(
'Demo.Animals.Pet',Demo.Animals.Animal,Demo.Animals.Ipet);
}

Demo.Animals.Cat 
= function(friendlyName){
Demo.Animals.Cat.initializeBase(
this, ['Cat', friendlyName]);
}

Demo.Animals.Cat.registerClass(
'Demo.Animals.Cat', Demo.Animals.Pet);
Demo.Animals.Cat.prototype.speak 
= function() {
    alert(
'meow');
}

Demo.Animals.Cat.prototype.toStringCustom 
= function() {
    
return 'Pet ' + Demo.Animals.Cat.callBaseMethod(this'toStringCustom');
}

Demo.Animals.Felix 
= function() {
    Demo.Animals.Felix.initializeBase(
this, ['Felix']);
}

Demo.Animals.Felix.registerClass(
'Demo.Animals.Felix', Demo.Animals.Cat);
Demo.Animals.Felix.prototype.toStringCustom 
= function() {
    
return Demo.Animals.Felix.callBaseMethod(this'toStringCustom'+ '  its Felix!';
}


Demo.Animals.Dog 
= function(friendlyName) {
    Demo.Animals.Dog.initializeBase(
this, ['Dog', friendlyName]);
}

Demo.Animals.Dog.registerClass(
'Demo.Animals.Dog', Demo.Animals.Pet);
Demo.Animals.Dog.prototype.speak 
= function() {
    alert(
'woof');
}


Demo.Animals.Tiger 
= function() {
    Demo.Animals.Tiger.initializeBase(
this, ['Tiger']);
}

Demo.Animals.Tiger.registerClass(
'Demo.Animals.Tiger', Demo.Animals.Animal);
Demo.Animals.Tiger.prototype.speak 
= function() {
    alert(
'grrr');
}

    
</script>
 
<body>
  This is my HTML page.
  
<br>
 
</body>
</html>
但是奇怪的是
a =new  Demo.Animals.Animal("cat");
alert(a.getName());
居然不会出错,也就是说虚类是可以实例化的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值