构造函数和析构函数在实际应用中用得多吗? 各位朋友:小弟是一位C#的入门者,现在正在看C#的教程,其中有一篇是讲关于“构造函数和析构函数”,但感觉上讲得比较深奥,想请教一下各位朋友,这两种函数在实际应用中究竟广不广泛?如果广泛的话,希望能举一些实际的例子,该在什么场合用到这两种函数,因为小弟的目的主要是作WEB开发,希望可以学到更多的知识,请各位朋友赐教,谢谢!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 构造函数会用得比较多比如一个类负责存放信息一般的做法是做成属性 NEW该类后.属性进行读写,也可以在构造函数里重载以参数直接指定 public class Student{ private string m_StuName = ""; private int m_Age = 0; public Student(); public Student(string stuName,int age) { this.m_StuName = stuName; this.m_Age = age; } public StudentName { get{return this.m_StuName;} set{this.m_StuName =value;} } public Age { get{return this.m_Age;} set{this.m_Age = value;} } 写错了...public class Student{ private string m_StuName = ""; private int m_Age = 0; public Student(); public Student(string stuName,int age) { this.m_StuName = stuName; this.m_Age = age; } public string StudentName { get{return this.m_StuName;} set{this.m_StuName =value;} } public int Age { get{return this.m_Age;} set{this.m_Age = value;} }}外面调用的时候有两种方式比如Student clsStudent = new Student();clsStudent.StudentName = "NPC1";clsStudent.Age = 11;或者Student clsStudent = new Student("NPC1",11); 构造函数是经常用的,定义一个类,系统都会生成一个默认的构造函数(不带参数),自已也可以写带参数的构造函数.例如建一个Form1窗口public class Form1: System.Windows.Forms.Form{ private string UserName; //用户名 public Form1() //C#自动生成的造函数(不带参数) { InitializeComponent(); //初始化 } public Form1(string userName) //自定义的造函数(有一个参数,接收用户名) { InitializeComponent(); //初始化 UserName = userName; //给UserName赋值,接收用户名 } ......}当其他地方要调用这个窗体, 实例化这个窗体时Form1 newForm = new Form1(); //实例化这个窗体,调用没参数的造函数Form1 newForm = new Form1("bigeyescat"); //实例化时,调用一个参数的造函数,把用户名传入Form1析构函数用得少,主要用来操作当类实例化后,用完了要释放时执行的操作。因为.NET有垃圾回收器,有Dispose方法等一般不用自已去写析构函数,让.NET自动去完成就可以了。除非你有特殊的操作 一个棘手的正则问题 DataView排序问题 csc无法编译cs文件问题 无法为Visual Studio创建"应用程序数据" 文件夹 怎么样将byte[]里的emf格式的图片读取到matefile中 请解释一下几个在连接sql数据库的几个单词的意思和用法 c#求助 asp.net + sql2012 C#如何调用可执行程序并给参数? 求快速统计海量数据中符合条件的记录数量 简单的c#程序 我在自己的类中定义一个TcpClient myClient;变量,连接server,然后发送数据,如果连接一段时间后
比如一个类负责存放信息
一般的做法是做成属性 NEW该类后.属性进行读写,
也可以在构造函数里重载以参数直接指定
{
private string m_StuName = "";
private int m_Age = 0;
public Student();
public Student(string stuName,int age)
{
this.m_StuName = stuName;
this.m_Age = age;
}
public StudentName
{
get{return this.m_StuName;}
set{this.m_StuName =value;}
}
public Age
{
get{return this.m_Age;}
set{this.m_Age = value;}
}
public class Student
{
private string m_StuName = "";
private int m_Age = 0;
public Student();
public Student(string stuName,int age)
{
this.m_StuName = stuName;
this.m_Age = age;
}
public string StudentName
{
get{return this.m_StuName;}
set{this.m_StuName =value;}
}
public int Age
{
get{return this.m_Age;}
set{this.m_Age = value;}
}
}
外面调用的时候有两种方式
比如
Student clsStudent = new Student();
clsStudent.StudentName = "NPC1";
clsStudent.Age = 11;或者
Student clsStudent = new Student("NPC1",11);
public class Form1: System.Windows.Forms.Form
{
private string UserName; //用户名 public Form1() //C#自动生成的造函数(不带参数)
{
InitializeComponent(); //初始化
} public Form1(string userName) //自定义的造函数(有一个参数,接收用户名)
{
InitializeComponent(); //初始化
UserName = userName; //给UserName赋值,接收用户名
}
......
}
当其他地方要调用这个窗体, 实例化这个窗体时
Form1 newForm = new Form1(); //实例化这个窗体,调用没参数的造函数
Form1 newForm = new Form1("bigeyescat"); //实例化时,调用一个参数的造函数,把用户名传入Form1析构函数用得少,主要用来操作当类实例化后,用完了要释放时执行的操作。因为.NET有垃圾回收器,有Dispose方法等一般不用自已去写析构函数,让.NET自动去完成就可以了。除非你有特殊的操作