首页 新闻 赞助 找找看

c#为啥类里面可以声明类?没碰到过

0
悬赏园豆:5 [已解决问题] 解决于 2017-03-07 12:57
 1     // 热水器
 2     public class Heater
 3     {
 4         private int temperature;
 5         public string type = "RealFire 001";       // 添加型号作为演示
 6         public string area = "China Xian";         // 添加产地作为演示
 7         //声明委托
 8         public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e);
 9         public event BoiledEventHandler Boiled; //声明事件
10 
11         //对下面的这个类不懂啊???
12         // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息
13         public class BoiledEventArgs : EventArgs   //这个为啥类里面可以声明类,以前没见过??
14         {
15             public readonly int temperature;
16             public BoiledEventArgs(int temperature)
17             {
18                 this.temperature = temperature;
19             }
20         }
21 
22         // 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视
23         protected virtual void OnBoiled(BoiledEventArgs e)
24         {
25             if (Boiled != null)
26// 如果有对象注册
27                 Boiled(this, e);  // 调用所有注册对象的方法
28             }
29         }

    // 热水器
    public class Heater
    {
        private int temperature;
        public string type = "RealFire 001";       // 添加型号作为演示
        public string area = "China Xian";         // 添加产地作为演示
        //声明委托
        public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e);
        public event BoiledEventHandler Boiled; //声明事件


        // 定义BoiledEventArgs类,传递给Observer所感兴趣的信息
        public class BoiledEventArgs : EventArgs   //这个为啥类里面可以声明类,以前没见过??
        {
            public readonly int temperature;
            public BoiledEventArgs(int temperature)
            {
                this.temperature = temperature;
            }
        }

        // 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视
        protected virtual void OnBoiled(BoiledEventArgs e)
        {
            if (Boiled != null)
            { // 如果有对象注册
                Boiled(this, e);  // 调用所有注册对象的方法
            }
        }

  //……其他方法

 }

AI大胜的主页 AI大胜 | 初学一级 | 园豆:122
提问于:2017-03-06 23:12
< >
分享
最佳答案
-1

买本语法书吧.淘宝一本也就2-30块钱.不然还会有更多这样的问题.

这个在书里叫做:内部类.

吴瑞祥 | 高人七级 |园豆:29449 | 2017-03-07 10:53

好的,内部类,我知道了,会去查一下,谢谢

AI大胜 | 园豆:122 (初学一级) | 2017-03-07 12:32
其他回答(3)
0

你在多看看书吧

Mundo Novo | 园豆:69 (初学一级) | 2017-03-07 01:24
0

没有为啥,语言就这么设计的。

你用过只是一直没发现而已,比如WinForm中的Controls集合。

设计的意义你可以参见上述举例用到之处。

收获园豆:5
花飘水流兮 | 园豆:13560 (专家六级) | 2017-03-07 01:26
0

涉及到c#嵌套类的知识,简单的介绍:http://www.cnblogs.com/qingyuan/archive/2010/08/25/1808177.html

AI大胜 | 园豆:122 (初学一级) | 2017-03-07 12:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册