using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace WindowsApplication1 { public partial class FormBase : Form { private static bool isClose;
public FormBase() { InitializeComponent(); isClose = false; }
//二. 被调用子窗体 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace WindowsApplication1 { public partial class Form2 : FormBase { private static Form2 f2;
//三. 主窗体 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;
namespace WindowsApplication1 { public partial class Form1 : FormBase { private FormBase f2;
《Beginning C# Object》
为什么源代码里没有实例化它的语句,它就能使用了?
而是先要form2 f2 = new form2();
因为.net将所有的东西都看成对象,对象必须要通过"构造方法"创建后,才能使用,从系统的角度看更加好, Java也是这样做的..
----------------------------------------------------类的静态构造函数有什么用?要设定类的字段初始值,直接设定不就行了?类没有"静态构造函数",所有的构造函数都是对一个类实例化,系统为这个实例分配内存,对某些字段初始化(如果有), 并压入堆栈,所有对象必须经过构造才能使用..
有一种静态类(static声明)的对象是不能实例化的,但如果你用"new"创建实例编译器也不会报错,Java就是这样的,但是static class本身并不等同于任何实例.其实你拉控件的时候系统也是调用了"构造函数"的,只是系统帮你做了你看不见..
当然你也可以在属性面板"直接设定",这样做有时比较方便..
为什么源代码里没有实例化它的语句,它就能使用了?
-----------------------------------------------------
看看主方法
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmInvoiceClass());
}
有没有实例化?为什么不能直接用form2.Show();而是先要form2 f2 = new form2();
------------------------------------------
不要用VB6的思路来想C#,这是一个误区,我也曾先会VB6,用习惯了VB,在C#中觉得一点也没有VB方便,但同时VB也是面向初学者,简单是一个主要特证,比如 VB中就可以这样写
form2.show();或者 new form2().show() 这两种都可以的。
{
static a() { aa = 999; }
internal static int aa=888;
}----------------------------------------------------yumanqing(每天进步一点点) 既然form1已经实例化,为什么不能用form1.Show();?
2、静态构造函数无参数,且访问修饰符存在与否没意义:由.NET运行库调用
3、静态构造函数使用的原因:在第一次使用类时,从外部源中初始化某些静态字段方法
4、静态构造函数的执行:.NET运行库不能确保其执行时间,但必定是在代码引用类前执行,且仅仅只一次
//一. 基类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class FormBase : Form
{
private static bool isClose;
public FormBase()
{
InitializeComponent();
isClose = false;
}
public bool IsClose
{
get { return isClose; }
}
private void FormBase_FormClosed(object sender, FormClosedEventArgs e)
{
isClose = true;
}
}
}
//二. 被调用子窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : FormBase
{
private static Form2 f2;
private Form2()
{
InitializeComponent();
}
private Form2(Form1 f1):base()
{
InitializeComponent();
}
public static Form2 GetF2(Form1 f1)
{
if (f2 == null)
{
f2 = new Form2(f1);
}
return f2;
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
f2 = null;
}
}
}
//三. 主窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : FormBase
{
private FormBase f2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (f2 == null || f2.IsClose )
{
f2 = Form2.GetF2(this);
f2.Show();
return;
}
if (f2.WindowState == FormWindowState.Minimized)
f2.WindowState = FormWindowState.Normal ;
f2.Show();
f2.Activate();
}
}
}
-------------------------------------------------------------------实例化的目的,简单的说,就是从同一个类,为了构造不同特性的实体,才要实例化.
在构造函数里为类的属性给值.
---------------------------
实例化了呀,
虽然不太理解你的本意,但是你的回复让我找到了答案。baseyueliang 和 Camus_gzw_02后面的回答
你们的话让我明白一些基本的,但是对我来说很模糊的概念~~-------------------------------------------------------thanks all!^_^