要在属于同一种类的对象之间共享数据值,可以使用static。
namespace demo3{ class point //定义点 { public int x; public int y; } class line { static public point origin = new point(); public point ending = new point(); } class Program { static void Main(string[] args) { line lin1 = new line(); line lin2 = new line(); line.origin.x = 1; line.origin.y = 5; lin1.ending.x = 6; lin1.ending.y = 7; lin2.ending.x = 8; lin2.ending.y = 9; Console.WriteLine("1开始点 {0}{1}",line.origin.x,line.origin.y); Console.WriteLine("1结束点 {0} {1}",lin1.ending.x,lin1.ending.y); Console.WriteLine("2开始点 {0}{1}", line.origin.x, line.origin.y); Console.WriteLine("2结束点 {0} {1}", lin2.ending.x, lin2.ending.y); line.origin.x = 999; line.origin.y = 888; Console.WriteLine("1开始点 {0}{1}", line.origin.x, line.origin.y); Console.WriteLine("1结束点 {0} {1}", lin1.ending.x, lin1.ending.y); Console.WriteLine("2开始点 {0}{1}", line.origin.x, line.origin.y); Console.WriteLine("2结束点 {0} {1}", lin2.ending.x, lin2.ending.y); Console.ReadKey(); } }}