《21天学通C#》上的练习题,有很多问题,希望得到指教。给下面的point结构和line结构加上属性:
//line.cs - calculate the distance of two point
//题目
//---------------------------------------------------using System;
struct point
{
public int x;
public int y;
}struct line
{
public point starting;
public point ending; public double length()
{
double len = 0;
len = Math.Sqrt((ending.x- starting.x)*(ending.x - starting.x)+ (ending.y- starting.y)*(ending.y- starting.y));
return len;
}
}class lineapp
{
public static void Main()
{
line myline;
myline.starting.x = 1;
myline.starting.y = 4;
myline.ending.x = 10;
myline.ending.y = 11; Console.WriteLine("Point1:{0},{1}",myline.starting.x,myline.starting.y);
Console.WriteLine("Point2:{0},{1}",myline.ending.x,myline.ending.y);
Console.WriteLine("Length of line from Point1 to Point2:{0}",myline.length());
}
}
解答:
首先给point结构加上属性,将x,y改为私有,然后用属性方法get和set.
//line.cs - calculate the distance of two point
//给point结构加上属性
//---------------------------------------------------using System;
struct point
{
int x;
int y;
public int my_x
{
get
{
return my_x;
}
set
{
my_x = value;
}
}
public int my_y
{
get
{
return my_y;
}
set
{
my_y = value;
}
}
}struct line
{
public point starting;
public point ending; public double length()
{
double len = 0;
len = Math.Sqrt((ending.my_x- starting.my_x)*(ending.my_x - starting.my_x)+ (ending.my_y- starting.my_y)*(ending.my_y- starting.my_y));
return len;
}
}class lineapp
{
public static void Main()
{
line myline;
myline.starting.my_x = 1;
myline.starting.my_y = 4;
myline.ending.my_x = 10;
myline.ending.my_y = 11; Console.WriteLine("Point1:{0},{1}",myline.starting.my_x,myline.starting.my_y);
Console.WriteLine("Point2:{0},{1}",myline.ending.my_x,myline.ending.my_y);
Console.WriteLine("Length of line from Point1 to Point2:{0}",myline.length());
}
}
编译出现错误:
---------- Compile ----------
Microsoft (R) Visual C# 2005 编译器 版本 8.00.50727.1433
用于 Microsoft (R) Windows (R) 2005 Framework 版本 2.0.50727
版权所有 (C) Microsoft Corporation 2001-2005。保留所有权利。Question1.cs(57,38): error CS0170: 使用了可能未赋值的字段“starting”
Question1.cs(58,38): error CS0170: 使用了可能未赋值的字段“ending”Output completed (0 sec consumed)
问什么说未赋值呢?starting和ending不是已经赋值了吗,他们的x,y都通过属性方法得到值了。
那么如果给starting和ending赋值呢?
我想到给struct point做一个构造方法,在struct line定义中对starting和ending在实例化时调用point结构的构造函数进行初始化。
//line.cs - calculate the distance of two point
//---------------------------------------------------using System;
struct point
{
int x;
int y;
public int my_x
{
get
{
return my_x;
}
set
{
my_x = value;
}
}
public int my_y
{
get
{
return my_y;
}
set
{
my_y = value;
}
}
public point(int x, int y)
{
this.x = x;
this.y = y;
}
}struct line
{
public point starting = new point(4,4);
public point ending = new point(5,5); public double length()
{
double len = 0;
len = Math.Sqrt((ending.my_x- starting.my_x)*(ending.my_x - starting.my_x)+ (ending.my_y- starting.my_y)*(ending.my_y- starting.my_y));
return len;
}
}class lineapp
{
public static void Main()
{
line myline;
myline.starting = new point(4,4);
myline.ending = new point(5,5);
myline.starting.my_x = 1;
myline.starting.my_y = 4;
myline.ending.my_x = 10;
myline.ending.my_y = 11; Console.WriteLine("Point1:{0},{1}",myline.starting.my_x,myline.starting.my_y);
Console.WriteLine("Point2:{0},{1}",myline.ending.my_x,myline.ending.my_y);
Console.WriteLine("Length of line from Point1 to Point2:{0}",myline.length());
}
}
编译出现错误:
---------- Compile ----------
Microsoft (R) Visual C# 2005 编译器 版本 8.00.50727.1433
用于 Microsoft (R) Windows (R) 2005 Framework 版本 2.0.50727
版权所有 (C) Microsoft Corporation 2001-2005。保留所有权利。Question2.cs(40,15): error CS0573: “line.starting”: 结构中不能有实例字段初始值设定项
Question2.cs(41,15): error CS0573: “line.ending”: 结构中不能有实例字段初始值设定项Output completed (0 sec consumed)
哦,结构只是一种数据类型,在没有实例化的时候,不能进行初始化的。
那么只好在Main()函数中来调用point结构的构造函数了。
//line.cs - calculate the distance of two point
//---------------------------------------------------using System;
struct point
{
int x;
int y;
public int my_x
{
get
{
return my_x;
}
set
{
my_x = value;
}
}
public int my_y
{
get
{
return my_y;
}
set
{
my_y = value;
}
}
public point(int x, int y)
{
this.x = x;
this.y = y;
}
}struct line
{
public point starting;
public point ending; public double length()
{
double len = 0;
len = Math.Sqrt((ending.my_x- starting.my_x)*(ending.my_x - starting.my_x)+ (ending.my_y- starting.my_y)*(ending.my_y- starting.my_y));
return len;
}
}class lineapp
{
public static void Main()
{
line myline;
myline.starting = new point(4,4);
myline.ending = new point(5,5);
myline.starting.my_x = 1;
myline.starting.my_y = 4;
myline.ending.my_x = 10;
myline.ending.my_y = 11; Console.WriteLine("Point1:{0},{1}",myline.starting.my_x,myline.starting.my_y);
Console.WriteLine("Point2:{0},{1}",myline.ending.my_x,myline.ending.my_y);
Console.WriteLine("Length of line from Point1 to Point2:{0}",myline.length());
}
}
现在编译没有错误了,但是运行报:
---------- Run ----------Process is terminated due to StackOverflowException.Output completed (16 sec consumed)
那么如何给point结构添加属性才是正确的呢?
同时,我还有一个疑问,line结构中的
{
   public point starting;
   public point ending;
}
这两个数据成员属于是声明还是实例化了呢?如果是实例化了,按照我的理解,两条语句调用了struct point的默认构造函数
,也进行了初始化,这不是与第一次编译的错误信息相矛盾了吗?

解决方案 »

  1.   

    楼主你的属性定义的就是错误的struct point
    {
        int x;
        int y;
        public int my_x
        {
            get
            {
                return my_x;
            }
            set
            {
                my_x = value;
            }
        }
        public int my_y
        {
            get
            {
                return my_y;
            }
            set
            {
                my_y = value;
            }
        }
    }应该修改为
    struct point
    {
        int x;
        int y;
        public int my_x
        {
            get{ return x;}
            set{ x = value;}
        }
        public int my_y
        {
            get { return y; }
            set { y = value; }
        }
    }
      

  2.   

    修改一下
    using System;
    struct point
    {
        int x;
        int y;
        public int my_x
        {
            get{ return x;}
            set{ x = value;}
        }
        public int my_y
        {
            get { return y; }
            set { y = value; }
        }
    }struct line
    {
        public point starting;
        public point ending;    public double length()
        {
            double len = 0;
            len = Math.Sqrt((ending.my_x - starting.my_x) * (ending.my_x - starting.my_x) + (ending.my_y - starting.my_y) * (ending.my_y - starting.my_y));
            return len;
        }
    }class lineapp
    {
        public static void Main()
        {
            //下面的new仅代表结构中的成员都使用默认值
            line myline = new line();
            myline.starting.my_x = 1;
            myline.starting.my_y = 4;
            myline.ending.my_x = 10;
            myline.ending.my_y = 11;        Console.WriteLine("Point1:{0},{1}", myline.starting.my_x, myline.starting.my_y);
            Console.WriteLine("Point2:{0},{1}", myline.ending.my_x, myline.ending.my_y);
            Console.WriteLine("Length of line from Point1 to Point2:{0}", myline.length());
        }
    }
      

  3.   

    还有楼主的编码习惯有点不接受,要我改应该是这样的using System;
    struct Point
    {
        private int x;
        private int y;
        public int X
        {
            get{ return x;}
            set{ x = value;}
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
    }struct Line
    {
        public Point Starting;
        public Point Ending;    public double GetLength()
        {
            double len = 0;
            len = Math.Sqrt((Ending.X - Starting.X) * (Ending.X - Starting.X) + (Ending.Y - Starting.Y) * (Ending.Y - Starting.Y));
            return len;
        }
    }class LineApp
    {
        public static void Main()
        {
            Line myline = new Line();
            myline.Starting.X = 1;
            myline.Starting.Y = 4;
            myline.Ending.X = 10;
            myline.Ending.Y = 11;        Console.WriteLine("Point1:{0},{1}", myline.Starting.X, myline.Starting.Y);
            Console.WriteLine("Point2:{0},{1}", myline.Ending.X, myline.Ending.Y);
            Console.WriteLine("Length of line from Point1 to Point2:{0}", myline.GetLength());
        }
    }