日期:2014-05-20  浏览次数:20768 次

苍天,大地!我出C#面试题,40个人,居然没有一个人完全答对,你呢
面试题:用C#编写一个桌子类,类中有三个属性:长、宽、高,一个方法,方法的功能为获得桌子的体积。

这是一道不好回答的题吗,有人告诉我吗?难到是我不应该出这样的题吗?到现在为止面试了差不多40人,为什么现在的计算机系的毕业生和知名IT培训企业出来的学生没有一个完全答对的呢,郁闷?有兴趣你也试试

------解决方案--------------------
C# code

class TableClass
{
    private float length;
    private float width;
    private float height;
    public float Length
    {
        get{return this.length;}
        set{this.length=value;}
    }
    public float Width
    {
        get{return this.width;}
        set{this.width=value;}
    }
    public float Height
    {
        get{return this.height;}
        set{this.height=value;}
    }

    public TableClass(float length,float width,float height)
    {
        if(length>0 && width>0 && height>0)
        {
            this.length=length;
            this.width=width;
            this.height=height;
        }
    }

    public float GetVoulumn()
    {
        return length*width*height;
    }
}

------解决方案--------------------
C# code

class  Table
{
    private float height;
    private float width;
    private float size;

    public Table()
    {
        height = width = size = 0;
    }

    public Table(float size, float width, float height)
    {
        this.height = height;
        this.width = width;
        this.size = size;
    }
    public float Height
    {
        get { return height; }
        set
        {
            if (height > 0)
            {
                height = value;
            }
        }
    }
    public float Width
    {
        get { return width; }
        set
        {
            if (width > 0)
            {
                width = value;
            }
        }
    }

    public float GetArea(Table table)
    {
        return table.Height * table.width * table.size;
    }


    public float Size
    {
        get { return size; }
        set
        {
            if (size > 0)
            {
                size = value;
            }
        }
    }
}

------解决方案--------------------
C# code
/*
面试题:用C#编写一个桌子类,类中有三个属性:长、宽、高,一个方法,方法的功能为获得桌子的体积。 
*/

class 桌子
{
  decimal l, w, h;
  public decimal 长 { get { return l; } set { l = value; } }
  public decimal 宽 { get { return w; } set { w = value; } }
  public decimal 高 { get { return h; } set { h = value; } }
  public decimal 体积() { return 长 * 宽 * 高; }
}