不知道如何定义方法,如何使用方法。特别是定义一个方法,调用该函数时以特定格式输出这个很蒙逼。
谢谢!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Q717058
{
class Point
{
private double X { get; set; }
private double Y { get; set; }
private double Z { get; set; }
private double T { get; set; }
public Point(double x, double y, double z, double t)
{
X = x; Y = y; Z = z; T = t;
}
public static implicit operator Point(string s)
{
double[] d = s.Split(' ').Select(x => double.Parse(x)).ToArray();
return new Point(d[0], d[1], d[2], d[3]);
}
public override string ToString()
{
return string.Format("({0},{1},{2},{3})", X, Y, Z, T);
}
public void Print()
{
Console.WriteLine(this);
}
public double Distance(Point p)
{
return Math.Sqrt(new double[] { X - p.X, Y - p.Y, Z - p.Z, T - p.T }
.Select(x => x * x)
.Sum());
}
public void Move(char c, double n)
{
var prop = GetType().GetProperty(c.ToString().ToUpper(), BindingFlags.NonPublic | BindingFlags.Instance);
if (prop != null)
prop.SetValue(this, (double)prop.GetValue(this, null) + n, null);
}
}
class Program
{
static void Main(string[] args)
{
Point p1 = (Point)Console.ReadLine();
Point p2 = (Point)Console.ReadLine();
string l3 = Console.ReadLine();
p1.Move(l3[0], double.Parse(l3.Split(' ')[1]));
p1.Print();
Console.WriteLine("Distance={0}", p1.Distance(p2));
}
}
}