用 TypeScript/Dart 来写 Go 的接收者
Write Receiver of Go in TypeScript/Dart Way
Go 接收者
package main
import (
"fmt"
"math"
)
type Vertex struct {
X, Y float64
}
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
func main() {
v := Vertex{3, 4}
fmt.Println(v.Abs())
}
TypeScript
class Vertex {
public x: number
public y: number
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
Abs(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
}
const v = new Vertex(3, 4);
console.log(v.Abs());
Dart
import 'dart:math';
class Vertex {
final double x;
final double y;
Vertex(this.x, this.y);
double Abs() {
return sqrt(this.x * this.x + this.y * this.y);
}
}
main() {
var v = Vertex(3, 4);
print(v.Abs());
}