关与C#的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/30 11:41:40
class window
{
private int nHeight = 0;
private int nWidth = 0;
public void setHeight(int Height)
{
if (Height <= 0)
nHeight = 500;
nHeight = Height;
}
public int getHeight()
{
if (nHeight <= 0)
nHeight = 100;
return nHeight;
}
public void setWidth(int Width)
{
if (Width <= 0)
nWidth = 500;
nWidth = Width;
}
public int getWidth()
{
if (nWidth <= 0)
nWidth =100;
return nWidth;
}
}

public partial class Default5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
window w = new window();
Response.Write("调用方法前宽度为:" + w.getWidth().ToString() +"高度为:" + w.getHeight().ToStrin

window w = new window(); //实例化,在window类中存在int nHeight,nWidth似有变量,getHeight(),getWidth()获取高和宽的两个方法,setHeight(),setWidth()设置高和宽的两个方法。
Response.Write("调用方法前宽度为:" + w.getWidth().ToString() +"高度为:" + w.getHeight().ToString()); //在页面中显示出宽和高的数值。由于你是第一次调用方法,那么输出结果应给为调用方法前宽度为:100高度为:100
w.setHeight(800);
w.setWidth(800); //通过方法传递值给私有变量,使其中有值。
Response.Write("<br>");
Response.Write("调用方法后宽度为:" + w.getWidth().ToString() + "高度为:" + w.getHeight().ToString()); //那么输出结果应给为调用方法前宽度为:800高度为:800

如果你要按照属性来写得话应该是:
private int nHeight = 0;

public int Height
{
get
{
if(nHeight <= 0 )
{
nHeight = 100;
}
return nHeight;
}
set
{
if (nHeight <= 0 )
{
nHeight = 500;
}
else
{
nHeigh