在派生类中引发基类事件分分快三计划

作者:编程技术

在基类中宣示能够从派生类引发的风浪的正经八百方法。此形式普遍应用于.Net Framework类库中的Windows窗体类。

更正后的代码如下:
基类:

作者们来看二个例子:

    public class BaseBusiness
    {
        //注明事件委托
        public delegate void ProgressEventHandler(int progress, string msg);
        //评释事件
        public event ProgressEventHandler ProgressEvent;
    }

    public class Button : ButtonBase, IButtonControl
    {
      protected override void OnClick(EventArgs e); //override基类的事件触发程序
    }

        protected virtual void OnProgress(int progress, string msg)
        {
            if (this.ProgressEvent != null)
                this.ProgressEvent(progress, msg);
        }
    }

在含有事件的基类中创制叁个受保险的调用方法。通过调用或重写方法,派生类便得以直接调用该事件。

本文地址:

public class Control : Component, IDropTarget, ISynchronizeInvoke, IWin32Window, IArrangedElement, IBindableComponent, IComponent, IDisposable
{
    public event EventHandler Click;   //基类中定义事件
    protected virtual void OnClick(EventArgs e);//protected封装事件触发程序,允许继承类调用或重写
}

    public class MyBusiness : BaseBusiness
    {
        public MyBusiness()
        {
            //  能够直接调用基类的措施
            this.OnProgress(this.OnProgressValue, "Testing...");            
        }

 

    public class BaseBusiness
    {
        //注解事件委托
        public delegate void ProgressEventHandler(int progress, string msg);
        //注脚事件
        public event ProgressEventHandler ProgressEvent;

namespace BaseClassEvents
{

    //Special EventArgs class to hold info about Shapes.
    public class ShapeEventArgs:EventArgs
    {
        private double newArea;
        public ShapeEventArgs (double d)
        {
            newArea = d;
        }
        public double NewArea
        {
            get
            {
                return newArea;
            }
        }
    }
    //Declare a delegate
    public delegate void CustomEventHandler(object sender, ShapeEventArgs e);

    //Base class event publisher
    public abstract class Shape
    {
        protected double area;
        public double Area
        {
            get
            {
                return area;
            }
            set
            {
                area = value;
            }
        }
        //raise an event
        public event CustomEventHandler ShapeChanged;

        public abstract void Draw();

        //event-invoking method
        protected virtual void OnShapeChanged(ShapeEventArgs e)
        {
            if(ShapeChanged !=null)
            {
                ShapeChanged(this, e);
            }
        }
    }
    public class Circle:Shape
    {
        private double radius;
        public Circle (double d)
        {
            radius = d;
            area = 3.14 * radius * radius;
        }
        public void Update(double d)
        {
            radius = d;
            area = 3.14 * radius * radius;
            OnShapeChanged(new ShapeEventArgs(area));
        }
        protected override void OnShapeChanged(ShapeEventArgs e)
        {
            base.OnShapeChanged(e);
        }
        public override void Draw()
        {
            Console.WriteLine("Drawing a circle");
        }
    }
    public class Rectangle:Shape
    {
        private double length;
        private double width;
        public Rectangle (double length,double width)
        {
            this.length = length;
            this.width = width;
            area = length * width;
        }
        public void Update(double length,double width)
        {
            this.length = length;
            this.width = width;
            area = length * width;
            OnShapeChanged(new ShapeEventArgs(area));
        }
        protected override void OnShapeChanged(ShapeEventArgs e)
        {
            base.OnShapeChanged(e);
        }
        public override void Draw()
        {
            Console.WriteLine("Drawing a rectangle");
        }
    }

    //Subscriber
    //Represents the surface on which the shapes are drawn
    //Subscibes to shape events so that it knows
    //when to redraw a shape
    public class ShapeContainer
    {
        List<Shape> _list;
        public ShapeContainer ()
        {
            _list = new List<Shape>();
        }
        public void AddShape(Shape s)
        {
            _list.Add(s);
            //Subscribe to the base class event.
            s.ShapeChanged  = HandleShapeChanged;
        }
        private void HandleShapeChanged(object sender,ShapeEventArgs e)
        {
            Shape s = (Shape)sender;
            Console.WriteLine("Received event. Shape area is now {0}", e.NewArea);
            s.Draw();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Create the event publishers and subscriber
            Circle c1 = new Circle(54);
            Rectangle r1 = new Rectangle(12, 9);
            ShapeContainer sc = new ShapeContainer();

            //Add the shapes to the container
            sc.AddShape(c1);
            sc.AddShape(r1);

            //Cause some events to be raised
            c1.Update(57);
            r1.Update(7, 7);

            Console.ReadKey();
        }
    }
}

        //  能够重写基类的办法
        protected override void OnProgress(int progress, string msg)
        {
            // 在这里间丰裕别的管理代码
            base.OnProgress(progress, msg);
        }
    }

我们通过ButtonBase最终找到基类Control

派生类:

 

    public class MyBusiness : BaseBusiness
    {
        public MyBusiness()
        {
            this.ProgressEvent(30,"Testing...");
        }
    }

咱俩来看三个Button类的概念

编写翻译时提示如下错误:   
事件“BaseBusiness.ProgressEvent”只可以冒出在 = 或 -= 的侧面(从种类“BaseBusiness”中利用时除了) 

派生类:

有下边意气风发段代码,目标是促成事件的存在延续:
基类:

要管理基类的风波,经常经过在基类中为事件创立受保证虚方法(On伊芙ntName)来兑现。派生类能够重写基类的OnEventName方法,也得以直接调用基类的OnEventName方法,进而完毕调用基类的平地风波。重写基类的OnEventName方法,派生类能够收获基类正在调用的事件,对这么些事件推行它自身的管理。

并发编译错误的始末是:
事件只可以从注明它们的类中调用,派生类无法一贯调用在基类表明的风云。

本文由分分快三计划发布,转载请注明来源

关键词: 分分快三计划 .NET 2..NET新手区