โค้ดที่เขียนนะครับ !!
var count = 0;
var counts = {};
function Bicycle(cadence,gear,speed){
this.cadence = cadence;
this.gear = gear;
this.speed = speed;
count++;
}
Bicycle.prototype = {
printStates:function(){console.log("cadence:" + this.cadence + " speed:" + this.speed + " gear:" + this.gear);},
applyBrake:function(decrement){this.speed -= decrement;},
speedUp:function(increment){this.speed += increment;},
setCadence:function(cadence){this.cadence=cadence;},
setGear:function(gear){this.gear=gear;},
setSpeed:function (speed){this.speed=speed;},
}
Bicycle.prototype.printTotalBike = function(){
console.log("There are " + count + " bike.");
}
MountainBike.prototype = Object.create(Bicycle.prototype);
TandemBike.prototype = Object.create(Bicycle.prototype);
function MountainBike(startCadence,startSpeed,startGear,startHeight) {
Bicycle.call(counts,startCadence,startSpeed,startGear);
this.cadence = startCadence;
this.speed = startSpeed;
this.gear = startGear;
this.setHeight = startHeight;
this.setHeight = function(height){
this.height=height;
};
this.printStates = function(){
console.log("and height:" + this.height);
};
}
function TandemBike(startCadence,startSpeed,startGear,seat) {
Bicycle.call(counts,startCadence,startSpeed,startGear);
this.cadence = startCadence;
this.speed = startSpeed;
this.gear = startGear;
this.numOfSeat = seat;
this.setNumOfSeat = function(numOfSeat) {
this.numOfSeat = numOfSeat;
};
this.printStates = function(printStates){
console.log("and numOfSeat:" + this.numOfSeat);
};
}
var mb = new MountainBike(0, 0, 1, 0);
mb.setCadence(10);
mb.setSpeed(20);
mb.setGear(4);
mb.setHeight(100);
mb.printStates();
var tb = new TandemBike(0, 0, 1, 2);
tb.setCadence(15);
tb.setSpeed(40);
tb.setGear(5);
tb.printStates();
Bicycle.prototype.printTotalBike();
/* ผลลัพธ์ */
and height:100
and numOfSeat:2
There are 2 bike.
ต้องการต่อข้อความ method จาก superclass
Moderator: mindphp
-
- Similar Topics
- ตอบกลับ
- แสดง
- โพสต์ล่าสุด
-
-
โพสต์ใหม่ 介绍 MZC Shipping Method 展示如何运输。
โดย alfie8522 » 07/07/2022 4:03 pm » ใน Chinese Language - 简体中文 - 0 ตอบกลับ
- 890 แสดง
-
โพสต์ล่าสุด โดย alfie8522
07/07/2022 4:03 pm
-
-
-
โพสต์ใหม่ ติดปัญหามรการเขียนส่งข้อมูล method post
โดย wkid » 17/06/2022 2:03 pm » ใน Programming - C/C++ & java & Python - 10 ตอบกลับ
- 1127 แสดง
-
โพสต์ล่าสุด โดย mindphp
18/06/2022 2:01 pm
-
-
- 0 ตอบกลับ
- 965 แสดง
-
โพสต์ล่าสุด โดย offing
17/01/2015 2:42 pm
-
- 0 ตอบกลับ
- 628 แสดง
-
โพสต์ล่าสุด โดย Ik Kat
26/07/2017 3:07 pm
ผู้ใช้งานขณะนี้
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 58