PHP 的 只读DTO
等级 5280 次按键
使用只读属性创建不可变的数据传输对象。
PHP 代码
1class Point { // 提升的readonly坐标:点不可修改2 public function __construct(3 public readonly float $x,4 public readonly float $y5 ) {}6 // 到另一点的欧几里得距离7 public function dist(Point $p): float { // 计算√((x1 − x2)² + (y1 − y2)²)8 return sqrt(9 ($this->x - $p->x) ** 210 + ($this->y - $p->y) ** 211 );12 }13}14 // 整数参数会被转换为float15$a = new Point(0, 0);16$b = new Point(3, 4);// √(9 + 16) = 5.0,echo输出d=517echo "d=" . $a->dist($b);运行结果
d=5
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 PHP 代码中需要输入的符号及其出现次数。
$× 11(× 8)× 8-× 7>× 5;× 4*× 4,× 3{× 3}× 3