js面向对象和继承
2016-05-05 08:20
441 查看
一个简单的题目:
写一个形状类,再写一个三角形类和一个矩形类,用面向对象的方式实现,输出各个形状的边数和面积。
三角形和矩形继承自形状类。
源码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
//-------------抽象类形状--------------
function
Shape(edges) {
this.edges
= edges;
}
//定义一个虚方法,用于重写
Shape.prototype.getArea
= function () {
return
-1;
}
Shape.prototype.getEdges
= function () {
return
this.edges;
}
//--------------三角形----------------
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;
}
//---------------矩形----------------
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;
}
//-------------测试-------------------
var
tri = new Triangle(4, 5);
document.write(tri.getEdges()
+ "<br>");
document.write(tri.getArea()
+ "<br>");
var
rect = new Rectangle(20, 40);
document.write(rect.getEdges()
+ "<br>");
document.write(rect.getArea()
+ "<br>");
</script>
</body>
</html>
写一个形状类,再写一个三角形类和一个矩形类,用面向对象的方式实现,输出各个形状的边数和面积。
三角形和矩形继承自形状类。
源码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
//-------------抽象类形状--------------
function
Shape(edges) {
this.edges
= edges;
}
//定义一个虚方法,用于重写
Shape.prototype.getArea
= function () {
return
-1;
}
Shape.prototype.getEdges
= function () {
return
this.edges;
}
//--------------三角形----------------
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;
}
//---------------矩形----------------
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;
}
//-------------测试-------------------
var
tri = new Triangle(4, 5);
document.write(tri.getEdges()
+ "<br>");
document.write(tri.getArea()
+ "<br>");
var
rect = new Rectangle(20, 40);
document.write(rect.getEdges()
+ "<br>");
document.write(rect.getArea()
+ "<br>");
</script>
</body>
</html>
相关文章推荐
- ajax小demo---CORS的原理分析及简单使用
- JavaScript 详说事件机制之冒泡、捕获、传播、委托
- cojs 榴莲 题解报告
- javascript闭包详解及常见陷进
- 浅析JS异步加载进度条
- 探讨:JavaScript ECAMScript5 新特性之get/set访问器
- javascript执行环境及作用域详解
- js ajaxfileupload.js上传报错的解决方法
- 详解Backbone.js框架中的模型Model与其集合collection
- 全面解析JavaScript的Backbone.js框架中的Router路由
- 实例讲解JavaScript的Backbone.js框架中的View视图
- javaScript中的原型解析【推荐】
- 详解JavaScript中基于原型prototype的继承特性
- 深入理解js promise chain
- 整理JavaScript对DOM中各种类型的元素的常用操作
- JS中dom0级事件和dom2级事件的区别介绍
- JavaScript编写检测用户所使用的浏览器的代码示例
- Sea.JS知识总结
- JavaScript中各种引用类型的常用操作方法小结
- ichart.js绘制虚线、平均分虚线效果的实现代码