用C#创建的Form,画一个按钮,看代码时实在搞不懂下面到底是什么意思.类form1中如下声名: private System.Windows.Forms.Button button1; //这里button1是个对像吗?而private void InitializeComponent()中又是如下处理的:this.button1 = new System.Windows.Forms.Button();//这里button1是个指针吗?为什么会这样?假设private System.Windows.Forms.Button button1;是一个对像,那么在初始化时,应该是this.button1.Create()来创建,既然C#中不区分指针与对像,却又使用new来创建,真是费解。
所以,你第一个问题答案是:对象.
第二:给button1这个对象分配内存空间.
this.button1 = new System.Windows.Forms.Button();//初始化,分配内存空间
和
private System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
是一样的。
Button button1= new Button();那么,System.Windows.Forms.Button()是什么?不是Button类的静态构造函数吗?如果button1是一个对像,那么可以用自己的成员函数来处理,比如this.button1.Button(),为什么还要用new来分配?new产生了一个对像,赋给了button1,而button1又是一个对像,原指向的对像不就找不到了?内存泄露?
定義變量。
this.button1 = new System.Windows.Forms.Button();
為變量分配內存空間。
如果说private System.Windows.Forms.Button button1;仅仅是声名变量,那么下面:
string string1;
这句又如何解释?从来没见哪里这样写:string1=new string();
第二个是分配内存空间,System.Windows.Forms.Button()不就是Button类的构造函数吗?类的成员private System.Windows.Forms.Button button1难道在构造时,没有执行构造函数Button?还需要单独来分配空间?
////
对了,只有new时才分配内存。前面只是告诉编译器。他是个什么类型的对象