说我有这个:
interface Point {
x: number;
y: number;
}
interface Line {
vertix1: Point;
vertix2: Point;
}
let v1: Point = { x: 1, y: 2 };
let v2: Point = { x: 1, y: 2 };
let line: Line = {vertix1: v1, vertix2: v2};
如何在不定义line
和v1
的情况下直接定义v2
?我试过,显然,这不起作用:
let line1: Line = {
vertix1: Point = { x: 1, y: 2 },
vertix2: Point = { x: 1, y: 2 },
}
答案 0 :(得分:1)
这只是:
let line1: Line = {
vertix1: { x: 1, y: 2 },
vertix2: { x: 1, y: 2 },
}
可以很容易地跳过
类。
答案 1 :(得分:0)
使用实现接口的类可能会更好。
interface IPoint {
x: number;
y: number;
}
interface ILine {
vertix1: Point;
vertix2: Point;
}
class Point implements IPoint {
x: int;
y: int;
constructor(x: int, y: int) {
this.x = x;
this.y = y;
}
}
class Line implments ILine {
vertix1: Point;
vertix2: Point;
constructor(vertix1: Point, vertix2: Point) {
this.vertix1 = vertix1;
this.vertix2 = vertix2;
}
}
let v1: Point = new Point(1,2);
let v2: Point = new Point(1,2);
let line: Line = new Line(v1,v2);
您还可以在构造函数中创建点。不确定这是否能回答你的问题,但希望它有所帮助。
let line: Line = new Line(new Point(1,2), new Point(1,2))