JavaScript——面向对象以及基于类的继承

本文介绍如何使用JavaScript通过prototype实现面向对象编程,包括创建交通工具基类和赛车子类,以及对象间的复制方法。基类包含车轮数和重量等属性及燃料和主要任务的方法,子类重写这些方法。
View Code
 1 /*###########################################*
 2  * 1.通过prototype建立面向对象的JavaScript
 3  * 2.基于类的继承
 4  ###########################################*/
 5 
 6 /*##################1#################*/
 7 /*创建交通工具的构造方法*/
 8 function Vehicle() {
 9     /*交通工具属性*/
10     var wheelCounts = 4;//车轮数
11     var curbWeightInPounds = 3000;//车子重量
12     
13     this.getWheelCounts = function() {
14         return wheelCounts;
15     } ;
16     this.getCurbWeightInPounds = function() {
17         return curbWeightInPounds;
18     } ;
19     
20     this.setWheelCounts = function(wc) {
21         wheelCounts = wc;
22     } ;
23     this.setCurbWeightInPounds = function(cwp) {
24         curbWeightInPounds = cwp;
25     } ;
26     //燃料
27     this.refueling = function() {
28         return "refueiing...";
29     };
30 
31     //主要任务
32     this.mainTasks = function() {
33         return "Go to school, shop and so on.";
34     };
35     
36 };
37 
38 
39 
40 /*赛车*/
41 function SPortsCar() {
42 
43     //燃料
44     SPortsCar.prototype.refueling = function() {
45         return "SPortsCar refueiing 3000";
46     };
47 
48     //主要任务
49     SPortsCar.prototype.mainTasks = function() {
50         return "SPortsCar driving, loking good, driving beach. ";
51     };
52     
53 };
54 
55 /*对象之间的复制**/
56 function copyObject() {
57     
58     this.createCopyObject = function (parent, child) {
59         for(var prototype in parent)
60         {
61             if(!child[prototype]){
62                 child[prototype] = parent[prototype];
63             }
64         };
65     };
66     
67 }

 

转载于:https://www.cnblogs.com/FCWORLD/archive/2013/01/25/2877112.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值