析构函数而如终结器,用于析构类的实例。
定义
析构函数(destructor)
与构造函数反而,当目标了该生命周期不时(例如对象所当的函数已调用了),系统活动执行析构函数。析构函数往往用来开“清理善后”
的干活(例如当树目标时用new开辟了平等片内存空间,delete会自动调用析构函数后获释内存)。
析构函数简介
坐C++语言为条例:\[1\]
析构函数名为吧答应同类名相同,只是于函数叫作前加一个位取反符~,例如~stud(
),以界别为构造函数。它不可知带任何参数,也从不返回值(包括void类型)。只能有一个析构函数,不克重载。如果用户没有编制析构函数,编译系统会面自动生成一个缺失省之析构函数(即使从定义了析构函数,编译器呢总是会否我们合成一个析构函数,并且只要由定义了析构函数,编译器在履行时见面先调用由定义之析构函数再调用合成的析构函数),它吗不进行其他操作。所以广大简的类吃无用显式的析构函数。
析构函数的行使
未克于构造面临定义析构函数。只能对类使用析构函数。
一个类似只能发出一个析构函数。
没辙持续或重载析构函数。
没辙调用析构函数。它们是让自动调用的。
析构函数既没有修饰符,也不曾参数。
声明:
class Car
{
~ Car() // destructor
{
// cleanup statements...
}
}
该析构函数隐式地针对目标的基类调用
Finalize.aspx)。这样,前面的析构函数代码被隐式地换为:
protected override void Finalize()
{
try
{
// cleanup statements...
}
finally
{
base.Finalize();
}
}
这意味对继承链中的备实例递归地调用 Finalize
方法(从派生程度极其老之至派生程度最小的)。
![]() |
---|
不应使用空析构函数。如果类包含析构函数, |
程序员无法控制何时调用析构函数,因为就是由于垃圾回收器决定的。垃圾回收器检查是否存在应用程序不再采用的靶子。如果垃圾回收器认为有对象符合析构,则调用析构函数(如果有)并回收用来囤此目标的内存。程序退出时也会调用析构函数。
得经过调用
Collect.aspx)
强制进行垃圾回收,但大多数景象下承诺避免这样做,因为这样见面导致性问题有关重新多信息,请参见强制垃圾回收.aspx)。
使用析构函数放资源
通常,与运作时无开展垃圾回收的编程语言相比,C#
无需太多的内存管理。这是为 .NET Framework
垃圾回收器会隐式地管理对象的内存分配和假释。但是,当应用程序封装窗口、文件及网络连接这看似非托管资源时,应当用析构函数放这些资源。当对象符合析构时,垃圾回收器将运行目标的
Finalize
方法。
资源的显式释放
若你的应用程序在利用昂贵之外表资源,则还建议您提供平等栽于垃圾堆回收器释放对象前显式地释放资源的计。可通过兑现自
IDisposable.aspx)
接口的 Dispose
方法来好就或多或少,该方式也目标实施必要的清理。这样只是大大加强应用程序的习性。即使出这种针对资源的显式控制,析构函数也是一律种植保护措施,可用来当对
Dispose
方法的调用失败时清理资源。
示例
下面的以身作则创建三只八九不离十,这三独八九不离十构成了一个继承链。类
First
是基类,Second
是从 First
派生的,而 Third
是从 Second
派生的。这三单近乎都发析构函数。在 Main()
中,创建了派生程度最可怜之类似的实例。注意:程序运行时,这三独像样的析构函数将机关为调用,并且是本从派生程度极其特别之至派生程度极其小的次第调用。
class First
{
~First()
{
System.Console.WriteLine("First's destructor is called");
}
}
class Second: First
{
~Second()
{
System.Console.WriteLine("Second's destructor is called");
}
}
class Third: Second
{
~Third()
{
System.Console.WriteLine("Third's destructor is called");
}
}
class TestDestructors
{
static void Main()
{
Third t = new Third();
}
}
本文章为List<T>单列集合开发品种,如要 Dictionary<K,V>双列聚开发的之路,请到楼主博客园寻找
博客网址:http://www.cnblogs.com/lsy131479/
窗体
一.首先定义项目类
/// <summary>
/// 项目类
/// </summary>
public class HealthCheckItem
{
//项目描述
private string description;
//项目名称
private string name;
//项目价格
private int price;
//无参构造
public HealthCheckItem()
{
}
//有参构造
public HealthCheckItem(string description, string name, int price)
{
this.description = description;
this.name = name;
this.price = price;
}
public string Description { get => description; set => description = value; }
public string Name { get => name; set => name = value; }
public int Price { get => price; set => price = value; }
}
二.定义套餐类
/// <summary>
/// 套餐类
/// </summary>
public class HealthCheckSet
{
//套餐名
private string name;
//套餐总价格
private int price;
//存储套餐内的项目
private List<HealthCheckItem> items = new List<HealthCheckItem>();
//无参构造
public HealthCheckSet()
{
}
//有参构造
public HealthCheckSet(string name)
{
this.name = name;
}
public string Name { get => name; set => name = value; }
public int Price { get => price; set => price = value; }
public List<HealthCheckItem> Items { get => items; set => items = value; }
}
三.主窗体代码
/// <summary>
/// 体检套餐管理系统 -- List<T>单列集合
/// </summary>
public FrmMain()
{
InitializeComponent();
}
//存储套餐类的集合
List<HealthCheckSet> Set = new List<HealthCheckSet>();
private void FrmMain_Load(object sender, EventArgs e)
{
//删除多余的列
this.dgvHealth.AutoGenerateColumns = false;
//删除多余的行
this.dgvHealth.AllowUserToAddRows = false;
/*
* 初始化套餐类集合
* 调用刷新datagridview的方法
* */
HealthCheckSet set0 = new HealthCheckSet("请选择");
HealthCheckSet set1 = new HealthCheckSet("入学体检");
Set.Add(set0);
Set.Add(set1);
AddExamCbo();
}
//存储所有套餐内项目的集合
public List<HealthCheckItem> allItems = new List<HealthCheckItem>();
public void AddPhyCbo()
{
//清空集合
allItems.Clear();
//集合初始化
HealthCheckItem item1 = new HealthCheckItem("用于检查身高。", "身高", 5);
HealthCheckItem item2 = new HealthCheckItem("用于检查体重。", "体重", 5);
HealthCheckItem item3 = new HealthCheckItem("用于检查肝功能。", "肝功能", 50);
HealthCheckItem item4 = new HealthCheckItem("用于检查视力。", "视力", 5);
HealthCheckItem item5 = new HealthCheckItem("用于检查听力。", "听力", 5);
HealthCheckItem item6 = new HealthCheckItem("用于检查B超。", "B超", 80);
HealthCheckItem item7 = new HealthCheckItem("用于检查心电图。", "心电图", 100);
allItems.AddRange(new HealthCheckItem[] { item1, item2, item3, item4, item5, item6, item7 });
/*
* 绑定刷新项目下拉框
* */
this.cboPhy.DisplayMember = "name";
this.cboPhy.DataSource = new BindingList<HealthCheckItem>(allItems);
}
public void AddExamCbo()
{
/*
* 绑定刷新套餐下拉框
* */
this.cboExams.DataSource = new BindingList<HealthCheckSet>(Set); ;
this.cboExams.DisplayMember = "name";
}
private void cboExams_SelectedIndexChanged(object sender, EventArgs e)
{
//调用绑定刷新datagridview方法
AddDgv();
//调用刷新金额的方法
CalcPrice();
//调用删除按钮状态方法
Btn();
/*
* 如果不是请选择也就是0下标还原初始状态(按钮为禁用,项目下拉框无项目)
* 反之,按钮解除禁用状态,项目下拉框从新填充数据
* */
if (this.cboExams.SelectedIndex > 0)
{
AddPhyCbo();
this.btnAdd.Enabled = true;
this.lblNames.Text = cboExams.Text;
}
else
{
this.btnAdd.Enabled = false;
this.btnDel.Enabled = false;
this.lblNames.Text = cboExams.Text;
this.cboPhy.DataSource = null;
}
}
public void AddDgv()
{
/*
* 将选中套餐的项绑定刷新datagridview
* */
foreach (HealthCheckSet item in Set)
{
if (item.Name.Equals(cboExams.Text))
{
this.dgvHealth.DataSource = new BindingList<HealthCheckItem>(item.Items);
return;
}
}
}
public void item()
{
foreach (HealthCheckSet item in Set)
{
if (item.Name.Equals(cboExams.Text))
{
foreach (HealthCheckItem it in item.Items)
{
if (it.Name.Equals(this.cboPhy.Text))
{
/*
* 判定是否已存在要添加的项
* */
MessageBox.Show("您已添加过此项!");
return;
}
}
foreach (HealthCheckItem it in allItems)
{
if (it.Name.Equals(this.cboPhy.Text))
{
/*
* 如果通过验证,可以添加,则将该项添加到集合中,并刷新datagridview与套餐价格
* */
item.Items.Add(it);
AddDgv();
CalcPrice();
MessageBox.Show("添加成功!");
return;
}
}
return;
}
}
}
public void CalcPrice()
{
foreach (HealthCheckSet item in Set)
{
if (item.Name.Equals(cboExams.Text))
{
/*
* 计算套餐金额
* */
item.Price = 0;
foreach (HealthCheckItem it in item.Items)
{
item.Price += it.Price;
}
//将套餐金额赋值给控件,并显示
this.lblPrices.Text = item.Price.ToString();
return;
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
//添加套餐项目
item();
Btn();
}
private void btnNew_Click(object sender, EventArgs e)
{
//非空验证
if (txtNewName.Text == "" || txtNewName.Text == null)
{
MessageBox.Show("请输入套餐名称!");
return;
}
/*
* 将通过验证的套餐名称添加到集合并刷新套餐下拉框
* */
HealthCheckSet set = new HealthCheckSet(this.txtNewName.Text);
Set.Add(set);
AddExamCbo();
this.cboExams.Text = this.txtNewName.Text;
MessageBox.Show("添加成功!");
}
private void btnDel_Click(object sender, EventArgs e)
{
foreach (HealthCheckSet item in Set)
{
if (item.Name.Equals(cboExams.Text))
{
for (int i = 0; i < item.Items.Count; i++)
{
if (item.Items[i].Name.Equals(this.dgvHealth.SelectedRows[0].Cells[0].Value.ToString()))
{
/*
* 删除要删除的项并刷新相关控件
* */
item.Items.RemoveAt(i);
AddDgv();
CalcPrice();
this.lblPrices.Text = item.Price.ToString();
MessageBox.Show("删除成功!");
Btn();
return;
}
}
return;
}
}
}
public void Btn()
{
foreach (HealthCheckSet item in Set)
{
if (item.Name.Equals(cboExams.Text))
{
/*
* 判断删除按钮的状态,如果集合里有数据为启用,无数据为禁用
* */
if (item.Items.Count > 0)
{
this.btnDel.Enabled = true;
}
else
{
this.btnDel.Enabled = false;
}
return;
}
}
}
}
本文章为 Dictionary<K,V>双列集合开发项目,如用List<T>单列集合开发之这路,请到楼主博客园寻找
博客网址:http://www.cnblogs.com/lsy131479/
窗体
一.首先创建项目类
public class HealthCheckItem
{
//项目描述
private string description;
//项目名称
private string name;
//项目价格
private int price;
//无参构造
public HealthCheckItem()
{
}
//有参构造
public HealthCheckItem(string description, string name, int price)
{
this.description = description;
this.name = name;
this.price = price;
}
public string Description { get => description; set => description = value; }
public string Name { get => name; set => name = value; }
public int Price { get => price; set => price = value; }
}
二.开立套餐类
/// <summary>
/// 套餐类
/// </summary>
public class HealthCheckSet
{
//套餐名
private string name;
//套餐总价格
private int price;
//存储套餐内的项目
private Dictionary<string, HealthCheckItem> items = new Dictionary<string, HealthCheckItem>();
//无参构造
public HealthCheckSet()
{
}
//有参构造
public HealthCheckSet(string name)
{
this.name = name;
}
public string Name { get => name; set => name = value; }
public int Price { get => price; set => price = value; }
public Dictionary<string, HealthCheckItem>{ get => items; set => items = value; }
}
三.主窗体代码
/// <summary>
/// 体检套餐管理系统 -- Dictionary<K,V>双列集合
/// </summary>
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void FrmMain_Load(object sender, EventArgs e)
{
//删除多余列
this.dataGridView1.AutoGenerateColumns = false;
//清楚多余行
this.dataGridView1.AllowUserToAddRows = false;
/*
* 套餐下拉框初始化
* */
HealthCheckSet set0 = new HealthCheckSet("请选择");
HealthCheckSet set1 = new HealthCheckSet("入学体检");
set.Add(set0.Name, set0);
set.Add(set1.Name, set1);
AddExamCbo();
}
//刷新项目下拉框
public void AddExamCbo()
{
BindingSource source = new BindingSource();
source.DataSource = set.Keys;
this.cboExams.DataSource = source;
}
//套餐集合
Dictionary<string, HealthCheckSet> set = new Dictionary<string, HealthCheckSet>();
//初始化项目集合
Dictionary<string, HealthCheckItem> allItems = new Dictionary<string, HealthCheckItem>();
public void AddPhyCbo()
{
/*
* 初始化(刷新)项目下拉框
* */
allItems.Clear();
HealthCheckItem item1 = new HealthCheckItem("用于检查身高。", "身高", 5);
HealthCheckItem item2 = new HealthCheckItem("用于检查体重。", "体重", 5);
HealthCheckItem item3 = new HealthCheckItem("用于检查肝功能。", "肝功能", 50);
HealthCheckItem item4 = new HealthCheckItem("用于检查视力。", "视力", 5);
HealthCheckItem item5 = new HealthCheckItem("用于检查听力。", "听力", 5);
HealthCheckItem item6 = new HealthCheckItem("用于检查B超。", "B超", 80);
HealthCheckItem item7 = new HealthCheckItem("用于检查心电图。", "心电图", 100);
allItems.Add(item1.Name, item1);
allItems.Add(item2.Name, item2);
allItems.Add(item3.Name, item3);
allItems.Add(item4.Name, item4);
allItems.Add(item5.Name, item5);
allItems.Add(item6.Name, item6);
allItems.Add(item7.Name, item7);
this.cboPhy.DisplayMember = "name";
BindingSource source = new BindingSource();
source.DataSource = allItems.Keys;
this.cboPhy.DataSource = source;
}
private void cboExams_SelectedIndexChanged(object sender, EventArgs e)
{
/*
* 改变下拉框下标,判断下标。改变状态
* */
this.lblNames.Text = this.cboExams.Text;
RenovateDgv();
Btn();
if (this.cboExams.SelectedIndex > 0)
{
this.btnAdd.Enabled = true;
AddPhyCbo();
}
else
{
this.btnAdd.Enabled = false;
this.cboPhy.DataSource = null;
}
}
private void btnNew_Click(object sender, EventArgs e)
{
//非空验证
if (txtNewName.Text == "" || txtNewName.Text == null)
{
MessageBox.Show("请输入套餐名称!");
return;
}
/*
* 集合添加套餐名
* 并刷新下拉框
* */
HealthCheckSet set3 = new HealthCheckSet(this.txtNewName.Text);
set.Add(set3.Name, set3);
AddExamCbo();
this.cboExams.Text = set3.Name;
MessageBox.Show("添加成功!");
}
public void AddDgv()
{
//ToList()将双列集合转为单列集合
//Contains()在单列集合中寻找是否已存在该项
if (set[cboExams.Text].Items.Keys.ToList().Contains(this.cboPhy.Text))
{
MessageBox.Show("您已添加过此项!");
return;
}
/*
* 向套餐内添加项目
* */
set[cboExams.Text].Items.Add(cboPhy.Text, allItems[cboPhy.Text]);
Btn();
MessageBox.Show("添加成功!");
}
public void RenovateDgv()
{
/*
* 刷新datagridview
* */
BindingSource source = new BindingSource();
source.DataSource = set[cboExams.Text].Items.Values;
dataGridView1.DataSource = source;
}
private void btnAdd_Click(object sender, EventArgs e)
{
/*
* 添加套餐项目
* */
AddDgv();
RenovateDgv();
}
public void Btn()
{
/*
* 由集合内是否有元素,来改变删除按钮的状态
* */
if (set[cboExams.Text].Items.Count > 0)
{
this.btnDel.Enabled = true;
}
else
{
this.btnDel.Enabled = false;
}
/*
* 计算套餐总金额
* 并刷新控件
* */
set[cboExams.Text].Price = 0;
foreach (KeyValuePair<string, HealthCheckItem> it in set[cboExams.Text].Items)
{
set[cboExams.Text].Price += it.Value.Price;
}
this.lblPrices.Text = set[cboExams.Text].Price.ToString();
}
private void btnDel_Click(object sender, EventArgs e)
{
/*
* 由键来移除指定的集合元素
* 并刷新控件显示的值与状态
* */
set[cboExams.Text].Items.Remove(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString().Trim());
RenovateDgv();
Btn();
MessageBox.Show("删除成功!");
}
}