java 编程创建一个Point类

java 编程创建一个Point类,第1张

public class Test102 {

public static void main(String[] args) {

Point point = new Point()

point.printPoint()

point.moveTo(1, 3)

System.out.println("--移动后--")

point.printPoint()

}

}

class Point {

private float x

private float y

public Point() {

this.x = 0

this.y = 0

}

/**

* 移动到点(dest1,dest2)

* @param dest1

* @param dest2

*/

public void moveTo(float dest1, float dest2) {

this.x = dest1

this.y = dest2

}

public void printPoint() {

System.out.print("当前点坐标:" + "(" + x + "," + y + ")\n")

}

}

class CPointEx

{

public:

CPointEx()

CPointEx(int x,int y,int z)

~CPointEx()

public:

CPointEx &operator=(CPointEx &p )

friend CPointEx operator+(CPointEx &p1,CPointEx &p2)

friend CPointEx operator-(CPointEx &p1,CPointEx &p2)

protected:

int m_x

int m_y

int m_z

}

CPointEx::CPointEx()

{

m_x = 0m_y=0m_z=0

}

CPointEx::CPointEx(int x,int y,int z)

{

m_x=x

m_y=y

m_z=z

}

CPointEx &CPointEx::operator=(CPointEx &p )

{

m_x=p.m_x

m_y=p.m_y

m_z=p.m_z

return *this

}

CPointEx operator+(CPointEx &p1,CPointEx &p2)

{

CPointEx p

p.m_x = p1.m_x+p2.m_x

p.m_y = p1.m_y+p2.m_y

p.m_z = p1.m_z+p2.m_z

return p

}

CPointEx operator-(CPointEx &p1,CPointEx &p2)

{

CPointEx p

p.m_x = p1.m_x-p2.m_x

p.m_y = p1.m_y-p2.m_y

p.m_z = p1.m_z-p2.m_z

return p

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7773163.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存