Attention!!如果定义的类名首字母大写,之后创建实例(对象)的时候都要首字母大写,反之统一为小写。如图
附完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 实验3
{
public partial class Form1 : Form
{
public class point
{
private double x;
private double y;
public double X
{
get { return x; }
set { x = value; }
}
public double Y
{
get { return y; }
set { y = value; }
}
public static double distance(Point p1,Point p2)
{
double result = 0;
result = Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y)*(p1.Y - p2.Y));
return result;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
point p1 = new point();
point p2 = new point();
double value = 0;
bool a=true;
try
{
p1.X = double.Parse(textBox1.Text);
p1.Y = double.Parse(textBox2.Text);
p2.X = double.Parse(textBox3.Text);
p2.Y = double.Parse(textBox4.Text);
value = point.distance(p1, p2);
label7.Text = value.ToString("0.00000");
}
catch (System.FormatException)
{
a = false;
MessageBox.Show("输入字符串格式错误");
}
finally
{
if(a==true)
MessageBox.Show("计算成功");
else
MessageBox.Show("计算失败");
}
}
private void label7_Click(object sender, EventArgs e)
{
}
}
}