【发布时间】:2020-08-04 13:39:43
【问题描述】:
class Point {
private x: number;
private y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
public distance(otherPoint: Point): number {
return Math.sqrt(
Math.pow(this.x - otherPoint.getX(), 2) +
Math.pow(this.y - otherPoint.getY(), 2));
}
public getX() {
return this.x;
}
public getY() {
return this.y;
}
}
class Circle {
private center: Point;
private radius: number;
constructor(center: Point, radius: number) {
this.radius = radius;
this.center = center;
}
public isInside(otherPoint: Point): boolean {
return this.center.distance(otherPoint) < this.radius;
}
}
class Triangle extends Point {
private z: number;
constructor(x:number, y: number, z: number){
super(x, y);
this.z = z;
}
public getZ(){
return this.z
}
public getPerimeter (otherPoint: Triangle): number{
return otherPoint.getX() + otherPoint.getY() + otherPoint.getZ()
}
}
let per = new Triangle(24, 61, 32);
console.log(per);
所以当我尝试编译时它说
private x: number;
^
SyntaxError: Unexpected identifier
【问题讨论】:
-
你是用 Typescript 编译的吗?
-
是的,我们的工作主要是使用私有类型脚本来完成
-
您正在使用 Typescript 语法编写代码,但我的意思是,您是使用 Typescript 编译器编译它还是使用
node命令运行它?您能否发布一下您是如何运行代码的? -
我用节点命令 node oop.ts -> 文件名运行它
-
好吧。你需要在你的机器上安装 Typescript 然后将你的 Typescript 代码编译成 Javascript ode 以便能够使用
node oop.ts
标签: javascript node.js typescript