用 JavaScript 建立一個投射物類來計算高度、水平距離和著陸點
問題
我們需要編寫一個 JavaScript 類:Projectile,該類在初始化時接收 3 個引數:-
- 起始高度 (0 ≤ h0 < 200)
- 起始速度 (0 < v0 < 200)
- 投射物的釋放角 (0° < a < 90°,以度為單位)。
我們需要為 Projectile 類編寫以下方法。
- 一個 horiz 方法,該方法接收引數 t,並計算投射物水平位移。(接收一個 double,返回一個 double)
示例
此類程式碼如下:
class Projectile{
constructor(h, u, ang){
this.h = h;
this.u = u;
this.ang = ang;
};
};
Projectile.prototype.horiz = function(t){
const dist = 2 * Math.cos(this.ang) * t;
return dist;
};
const p = new Projectile(5, 2, 45);
const horizontal = p.horiz(.2);
console.log(horizontal);輸出
輸出如下:
0.2101287955270919
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP