题目:
定义一个“点”(Point3D)类用来表示三维空间中的点(有三个坐标)。要求如下:
字段设为私有并为每个字段设置属性
可以生成具有特定坐标的点对象
提供可以设置三个坐标的方法
提供可以计算该“点”距原点距离平方的方法
编写程序验证上述三条
using System;
namespace ConsoleApp4_5
{
public class TPoint3
{
private int x, y, z;
public int px
{
get { return x; }
set { x = value; }
}
public int py
{
get { return y; }
set { y = value; }
}
public int pz
{
get { return z; }
set { z = value; }
}
/*
public int Read_x()
{
return x;
}
public void Write_x(int v)
{ x = v;}
*/
public double distance()
{
return Math.Sqrt(x * x + y * y + z * z);
}
};
class Program
{
static void Main(string[] args)
{
TPoint3 p = new TPoint3();
p.px = 3;
p.py = 4;
p.pz = 5;
p.distance();
Console.WriteLine("点=>({0},{1},{2})",p.px,p.py,p.pz);
double test = p.distance();
string s = test.ToString("0.00");//保留两位小数
Console.WriteLine("到原点的距离"+s);
}
}
}
运行结果: