对象的实例化的一点理解

01.using System;  
02.using System.Collections.Generic;  
03.using System.ComponentModel;  
04.using System.Data;  
05.using System.Drawing;  
06.using System.Linq;  
07.using System.Text;  
08.using System.Windows.Forms;  
09. 
10.namespace TestDemo  
11.{  
12.    public partial class Form1 : Form  
13.    {  
14.         
15.        Label lb = new Label();         
16.        Label aa;         
17.        public Form1()  
18.        {  
19.            InitializeComponent();  
20.              
21.        }  
22. 
23.        private void Form1_Load(object sender, EventArgs e)  
24.        {  
25.             
26.            lb.Text = "sdfsdfdfsdfsdfsdfsdf";  
27.            lb.Location = new Point(40, 50);  
28.            lb.Size = new Size(new Point(100, 10));  
29.            this.Controls.Add(lb);  
30.            this.Controls.Remove(lb);  
31. 
32.            lb.Text = "123";  
33.            lb.Location = new Point(60, 80);  
34.            lb.Size = new Size(new Point(100, 10));  
35.            this.Controls.Add(lb);  
36.            this.Controls.Remove(lb);  
37.              
38.            aa = new Label();  
39.            aa.Text = "888888";  
40.            aa.Name = "a1";  
41.            aa.Location = new Point(150, 100);  
42.            aa.Size = new Size(new Point(100, 10));  
43.            this.Controls.Add(aa);  
44.            this.Controls.Remove(aa);  
45.            aa = new Label();  
46.            aa.Text = "99999";                           //把aa.Text = "888888"覆盖,但 aa.Text = "888888"会在Form上留下痕迹  
47.            aa.Name = "a2";  
48.            aa.Location = new Point(200, 150);  
49.            aa.Size = new Size(new Point(100, 10));  
50.            this.Controls.Add(aa);  
51.            this.Controls.Remove(aa);  
52. 
53.            Label cc  = new Label();  
54.            cc.Text = "00000000";  
55.            cc.Name = "a3";  
56.            cc.Location = new Point(180, 120);  
57.            cc.Size = new Size(new Point(100, 10));  
58.            this.Controls.Add(cc);  
59. 
60. 
61.            //Label lb = new Label();  
62.            cc.Text = "00000000";  
63.            cc.Name = "a3";  
64.            cc.Location = new Point(180, 120);  
65.            cc.Size = new Size(new Point(100, 10));  
66.            this.Controls.Add(cc);  
67.            //this.Controls.Remove(cc);  
68.            this.Controls.Remove(this.textBox1);        
69.        }  
70. 
71.        private void button1_Click(object sender, EventArgs e)  
72.        {  
73.            //虽然lb在Form上别删除但是实际是并没有删除的。下面一句可以将该空间添加回来的(添加回来的空间是最后删除前控件的值)  
74.            //总结:定义为全局变量的对象在整个过程中都存在  
75.            //全局对象在定义的同时实例化和非实例化几乎是差不多的,在真个过程中只会有时实例。只是定义实体但不复制的实体会有“瞬间值”  
76.            //这个值将被下次复制而取代,实体将不再存在(但这个瞬间值会留下痕迹,在Form上显示,但是你不可能在找到它了)  
77.            //如果一直想对同一对象操作,而不希望看到其他的变化,可以用全局对象在定义同时并实例化  
78.            //如果一直想对同一对象操作,希望看到其他的变化,可以用全局对象在定义,在每次使用是单独实例化  
79.            if (lb != null)  
80.            {  
81.                this.Controls.Remove(lb);//前面删除了,竟然还可以删除,是因为是全局对象保存了值  
82.                this.Controls.Add(lb);   //添加可以,同上原因  
83.            }  
84.            if (aa != null)  
85.            {  
86.                this.Controls.Remove(aa);//同上  
87.                this.Controls.Remove(aa);//同上  
88.            }  
89.            this.Controls.Remove(textBox1);  
90.            this.Controls.Add(textBox1);   
91.        }        
92.    }  
93.} 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Joetao/archive/2010/09/04/5862982.aspx

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值