public partial class test01 : System.Web.UI.Page
{
class Farmer
{
private string name; //字段,一般为private,用于内部的数据传送
public Farmer() //无参数构造函数
{
name = "无参数构造函数!";
}
public Farmer(string FName) //有参数构造函数
{
name = FName;
}
public string Name //属性,前面加了public才能被外部访问
{
get
{
return name;
}
set
{
name = value;
}
}
public string NameAdd() //方法
{
name = name + ",李四";
return name;
}
public static string NameAddStatic() //静态方法,调用静态方法不需要实例化
{
Farmer myFarmer = new Farmer();
return myFarmer.Name+",这是静态方法!";
}
}
protected void Page_Load(object sender, EventArgs e)
{
Farmer farmer1 = new Farmer();
Label1.Text = farmer1.Name;
Farmer farmer2 = new Farmer("张三");
Label2.Text = farmer2.Name;
Farmer farmer3 = new Farmer("张三");
Label3.Text = farmer3.NameAdd();
Farmer farmer4 = new Farmer();
farmer4.Name = "你好";
Label4.Text=farmer4.NameAdd();
Label5.Text = Farmer.NameAddStatic(); //调用静态方法不需要实例化
}
}
{
class Farmer
{
private string name; //字段,一般为private,用于内部的数据传送
public Farmer() //无参数构造函数
{
name = "无参数构造函数!";
}
public Farmer(string FName) //有参数构造函数
{
name = FName;
}
public string Name //属性,前面加了public才能被外部访问
{
get
{
return name;
}
set
{
name = value;
}
}
public string NameAdd() //方法
{
name = name + ",李四";
return name;
}
public static string NameAddStatic() //静态方法,调用静态方法不需要实例化
{
Farmer myFarmer = new Farmer();
return myFarmer.Name+",这是静态方法!";
}
}
protected void Page_Load(object sender, EventArgs e)
{
Farmer farmer1 = new Farmer();
Label1.Text = farmer1.Name;
Farmer farmer2 = new Farmer("张三");
Label2.Text = farmer2.Name;
Farmer farmer3 = new Farmer("张三");
Label3.Text = farmer3.NameAdd();
Farmer farmer4 = new Farmer();
farmer4.Name = "你好";
Label4.Text=farmer4.NameAdd();
Label5.Text = Farmer.NameAddStatic(); //调用静态方法不需要实例化
}
}