如题所述
第1个回答 2022-10-28
类的设计思想:
构造函数,可以给点赋初值的构造函数。
点的相关参数作为成员变量,如横坐标,纵坐标。
设计成员函数,如取得坐标值的函数,此点和原点之间的距离。
in c language
123456789101112131415161718192021222324class Point{ protected: double x; double y; Point(inx = 0 , iny = 0){//C的缺省构造函数,构造不传入参数时默认值是0 x = inx; y = iny; } ~Point(){ } public: double getX(){ return x; } double getY(){ return y; } void setX(double inx){ x = inx; } void setY(double iny){ y = iny; }};//写C一定不要忘记,类定义结束也有个分号。in java language
1234567891011121314151617181920public class Point(){//JAVA对于类的属性要求很严苛,每个元素必须显式注明属性 protected double x; protected double y; public Point(double inx,double iny){ x = inx; y = iny; } public void setX(double inx){ x = inx; } public void setY(double iny){ y = iny; } public double getX(){ return x; } public double getY(){ return y; }}in JS
in PHP
123456789101112131415161718192021222324<?phpclass Point{ protected $x; protected $y; function __construct($inx,$iny){ $this->x = $inx; $this->y = $iny; } function __destruct(){} public function getX(){ return $this->x; } public function getY(){ return $this->y; } public function setX($inx){ $this->x = $inx; } public function setY(){ $this->y = $iny; }}?>