日期:2014-05-20 浏览次数:20807 次
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; } }
------解决方案--------------------
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#编写一个桌子类,类中有三个属性:长、宽、高,一个方法,方法的功能为获得桌子的体积。 */ 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 长 * 宽 * 高; } }