首页 新闻 会员 周边

问两个问题 一个是事件 一个是一段代码的意识 有点看不同

0
悬赏园豆:10 [已关闭问题]

我在看书的时候上面讲到委派和事件的时候我按书上做了下面的代码但是有问题请大家帮看看为什么那个地方提示为引用对象实例 不知道是什么原因 书上的代码就和我这个一样只是命名不同 代码如下 出问题的地方我在代码里面标记了 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eventTest
{
 class EventTest
 {
 }
    public class MeltdownEventArgsTest : EventArgs
    {
        private string message;
        public MeltdownEventArgsTest(string message)
        {
            this.message = message;
        }
        public string Message
        {
            get
            {
                return message;
            }
        }
    }
    public class ObjOne
    {
        private int old;
        public delegate void MeltdownHandler(object obj, MeltdownEventArgsTest Mt);
        public event MeltdownHandler OnMelt;
        public int Old
        {
            set
            {
                old = value;
                if (old > 1000)
                {
                    MeltdownEventArgsTest mt = new MeltdownEventArgsTest("温度过高");
                    OnMelt(this, mt);//在这个地方提示为引用对象实例。错误在这个地方
                }

            }
        }
    }
    public class readerObj
    {
        public readerObj(ObjOne oO)
        {
            oO.OnMelt+=new ObjOne.MeltdownHandler(oO_OnMelt);
        }
        public void oO_OnMelt(object obj, MeltdownEventArgsTest mt)
        {
            Console.Write(mt.Message);
        }
    }
}

 

下面这段代码是原先设计模式里面的一段代码 我不明白 ManageColor类 ,代码如下。

abstract class ColorPrototype

{

    public abstract ColorPrototype Clone();

}


class ConcteteColorPrototype : ColorPrototype

{


    private int _red, _green, _blue;

 

    public ConcteteColorPrototype(int red, int green, int blue)

    {

        this._red = red;

        this._green = green;

        this._blue = blue;

    }

     public override ColorPrototype Clone()

    {

        //实现浅拷贝

        return (ColorPrototype) this.MemberwiseClone();

    }


    public void Display(string _colorname)

    {

        Console.WriteLine("{0}'s RGB Values are: {1},{2},{3}",

            _colorname,_red, _green, _blue );

    }

}


class ColorManager

{

    Hashtable colors = new Hashtable();

//这里为什么可以ColorPrototype this 这样写 这样写又是什么意思

 

    public ColorPrototype this[string name]//这个地方我不明白为什么用中括号

    {

        get

        {

            return (ColorPrototype)colors[name];

        }

        set

        {

            colors.Add(name,value);

        }

    }

}

yzy的主页 yzy | 菜鸟二级 | 园豆:317
提问于:2009-09-28 23:52
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册