您能在下面为我解释一下这段代码吗 [英] Can you exlain for me this code below

查看:62
本文介绍了您能在下面为我解释一下这段代码吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ProgrammerGuide_001
{
    public class WinForm:Form
    {
        private Button button1;
        private TextBox textBox1;
        public WinForm()
        {
        }
        static void Main(string[] args)
        {
            WinForm myFrm = new WinForm();
            myFrm.button1 = new Button();
            myFrm.textBox1 = new TextBox();
            myFrm.Text = "my first window application";
            Application.Run(myFrm);
        }
    }
}



我有一个问题,当我这样声明2个对象变量时:



I have a question that when I declare 2 object variable like this:

private Button button1;
private TextBox textBox1;


为什么我无法使用以下语句实例化:


why I can''t instantiate with the following statements:

button1=new Button();
textBox1=new TextBox();


但是这段代码可以:


but this code can:

myFrm.button1 = new Button();
myFrm.textBox1 = new TextBox();

推荐答案

因为要在表单类的实例中实例化它们.这段代码大部分都是有延迟的,没有任何意义,一个基本的控制台应用程序是学习此概念的一种更好的方法,而不是那么分心.您处于静态方法中,它没有状态,因此对象不存在于main内部,而是仅存在于该类的INSTANCE内部.
Because you''re instantiating them inside the instance of the form class. This code is mostly retarded, it makes no sense, a basic console app is a better way to learn this concept, not as many distractions. You''re in a static method, it has no state, so the objects don''t exist inside main, they only exist inside an INSTANCE of the class.


这篇关于您能在下面为我解释一下这段代码吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆