javascript中实现继承的示例
来源:互联网 发布:捕蜂器淘宝 编辑:程序博客网 时间:2024/05/17 23:41
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>对象继承的示例</title>
<script type="text/javascript">
//对象继承的示例
//NaN 是 Not a Number的意思
function Shape(edge) {
this.edge = edge;
}
Shape.prototype.getArea = function() {
return -1;
}
//三角形
function Triangle(bottom, height) {
Shape.call(this, 3);
this.bottom = bottom;
this.height = height;
}
Triangle.prototype = new Shape();
Triangle.prototype.getArea = function() {
return 0.5 * this.bottom * this.height;
}
Triangle.prototype.getEdge = function() {
return this.edge;
}
var triangle = new Triangle(10, 4);
alert(triangle.getEdge());
alert(triangle.getArea());
//四边形
function Rectangle(bottom, height) {
Shape.call(this, 4);
this.bottom = bottom;
this.height = height;
}
Rectangle.prototype = new Shape();
Rectangle.prototype.getArea = function() {
return this.bottom * this.height;
}
Rectangle.prototype.getEdge = function() {
return this.edge;
}
var rectangle = new Rectangle(20, 40);
alert(rectangle.getEdge());
alert(rectangle.getArea());
</script>
</head>
<body>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>对象继承的示例</title>
<script type="text/javascript">
//对象继承的示例
//NaN 是 Not a Number的意思
function Shape(edge) {
this.edge = edge;
}
Shape.prototype.getArea = function() {
return -1;
}
//三角形
function Triangle(bottom, height) {
Shape.call(this, 3);
this.bottom = bottom;
this.height = height;
}
Triangle.prototype = new Shape();
Triangle.prototype.getArea = function() {
return 0.5 * this.bottom * this.height;
}
Triangle.prototype.getEdge = function() {
return this.edge;
}
var triangle = new Triangle(10, 4);
alert(triangle.getEdge());
alert(triangle.getArea());
//四边形
function Rectangle(bottom, height) {
Shape.call(this, 4);
this.bottom = bottom;
this.height = height;
}
Rectangle.prototype = new Shape();
Rectangle.prototype.getArea = function() {
return this.bottom * this.height;
}
Rectangle.prototype.getEdge = function() {
return this.edge;
}
var rectangle = new Rectangle(20, 40);
alert(rectangle.getEdge());
alert(rectangle.getArea());
</script>
</head>
<body>
</body>
</html>
0 0
- javascript中实现继承的示例
- javascript中实现继承的示例二
- javascript原型链简单示例(实现简单的继承)
- 关于 javascript 中 继承的 一个 小 示例
- javascript中实现类继承的方法
- JavaScript中继承的实现方法
- Javascript中继承的实现方法
- Javascript中继承的实现方法
- JavaScript中继承机制的实现
- Javascript中对象继承的实现
- JavaScript中实现继承的而方法
- 实现JavaScript的继承
- JavaScript继承的实现
- JavaScript继承的实现
- javascript 继承的实现
- JavaScript继承的实现
- javascript继承的实现
- javascript的继承实现
- hud 1532 Drainage Ditches + 网络流入门题
- java虚拟机读写其它进程的数据及RandomAccessFile
- Selenium2.0功能测试之唯一元素的定位
- 山东理工大学ACM平台题答案关于C语言 1196 排序问题
- Hadoop2.2.0的Eclipse插件
- javascript中实现继承的示例
- STL Vector
- eclipse中重构技巧
- ASIC的一些技巧和软件
- 基于HTML5技术的电力3D监控应用(二)
- java内部类 匿名内部类
- 关于operator const char *() const的问题
- 蜗牛—cocos2dx之初识
- 在Selenium WebDriver中使用By.Xpath快速定位页面元素