delegate

委托Delegate +事件Event

蹲街弑〆低调 提交于 2019-11-29 12:52:37
委托就是面向对象函数指针 能指向静态方法和对象的实例 委托必须跟预委托(方法)具有相同的返回值 参数类型 委托的本质 是一个类 任何声明类的地方都可以声明委托。 我们习惯于把数据作为参数传递给方法,如上面的例子所示。所以,给方法传递另一个方法听起来有点奇怪。而有时某个方法执行的操作并不是针对数据进行的,而是要对另一个方法进行操作。更麻烦的是,在编译时我们不知道第二个方法是什么,这个信息只能在运行时得到,所以需要把第二个方法作为参数传递给第一个方法 事件的四个步骤: 1: 在事件发行者中定义一个事件 2:在事件发行者中触发事件 3:在事件订阅者中定义事件处理程序 4:向事件发行者订阅一个事件 事件设计准则: 1:命名准则使用PascalCasing命名方式 2:声明delegate时,返回值必须是void,EventName事件的事件委托是EventNameEventHanlder,事件接受两个传入参数,一律命名为sender和e 3:定义一个提供事件委托的类,对类以EventNameArgs进行命名,从System.EventArgs派生该类,然后添加所有事件特定的成员。 public delegate void EventNameEventHanlder(object sender ,EventNameEventArgs e) 例子: ~using System; class

委托和事件[delegate and event]_C#

不羁岁月 提交于 2019-11-29 12:52:07
引用:[Mark Michaelis.Essential C#2.0] 委托和事件: 1. 委托 :一个能够表示 方法 的数据类型;它将方法作为对象封装起来,允许在运行时间接地绑定一个方法调用。 2. 声明委托数据类型: public delegate bool GreaterThanHandler( int first , int second); 3. 委托的实例化: 为了实例化委托,需要和委托类型自身的签名对应的一个方法;实例时 不必用new 来实例化该类的实例,直接传递名称即可[C#2.0新语法]。 如: GreaterThanHandler a = 方法名; C#2.0 以前的语法: GreaterThanHandler a = new GreaterThanHandler ( 方法名) ; 4. 匿名方法: 匿名方法没有实际方法声明的委托实例,它们的定义是直接内嵌在代码中的。如: GreaterThanHandler a = delegate (int first , int second){return (first<second);}; 5. 委托的内部机制: C# 将所有委托定义成间接派生于System.Delegate ,这个类有两个属性:(1)MethodInfo(System.Reflection.MethodInfo类型): 定义了一个特定方法签名

Delegate Demo

孤人 提交于 2019-11-29 12:47:29
//注:此例为完整实例. using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplications { public delegate void Counts(object sender, AreaEventArgs e); public class Core { public event Counts coArea; public void CountArea(int length, int width) { AreaEventArgs e = new AreaEventArgs(); e.Length = length; e.Width = width; if (coArea != null) { coArea(this, e); } } }    public class AreaEventArgs : EventArgs { private int _width; public int Width { get { return _width; } set { _width = value; } } private int _length; public int Length { get { return _length; } set { _length =

聊聊spring cloud的AbstractLoadBalancingClient

自作多情 提交于 2019-11-29 06:38:00
序 本文主要研究一下spring cloud的AbstractLoadBalancingClient AbstractLoadBalancingClient spring-cloud-netflix-ribbon-2.0.0.RELEASE-sources.jar!/org/springframework/cloud/netflix/ribbon/support/AbstractLoadBalancingClient.java /** * @author Spencer Gibb */ public abstract class AbstractLoadBalancingClient<S extends ContextAwareRequest, T extends IResponse, D> extends AbstractLoadBalancerAwareClient<S, T> implements ServiceInstanceChooser { protected int connectTimeout; protected int readTimeout; protected boolean secure; protected boolean followRedirects; protected boolean okToRetryOnAllOperations;

C#多线程编程

安稳与你 提交于 2019-11-29 06:06:01
一、使用线程的理由 1、可以使用线程将代码同其他代码隔离,提高应用程序的可靠性。 2、可以使用线程来简化编码。 3、可以使用线程来实现并发执行。 二、基本知识 1、进程与线程:进程作为操作系统执行程序的基本单位,拥有应用程序的资源,进程包含线程,进程的资源被线程共享,线程不拥有资源。 2、前台线程和后台线程:通过Thread类新建线程默认为前台线程。当所有前台线程关闭时,所有的后台线程也会被直接终止,不会抛出异常。 3、挂起(Suspend)和唤醒(Resume):由于线程的执行顺序和程序的执行情况不可预知,所以使用挂起和唤醒容易发生死锁的情况,在实际应用中应该尽量少用。 4、阻塞线程:Join,阻塞调用线程,直到该线程终止。 5、终止线程:Abort:抛出 ThreadAbortException 异常让线程终止,终止后的线程不可唤醒。Interrupt:抛出 ThreadInterruptException 异常让线程终止,通过捕获异常可以继续执行。 6、线程优先级:AboveNormal BelowNormal Highest Lowest Normal,默认为Normal。 三、线程的使用 线程函数通过委托传递,可以不带参数,也可以带参数(只能有一个参数),可以用一个类或结构体封装参数。 namespace Test { class Program { static

iOS WebRTC dataChannel 数据发送注意事项

橙三吉。 提交于 2019-11-29 05:47:40
1. 通道建立 1. 通道的时候,按照一般顺序,先创建工厂 _factory = [[RTCPeerConnectionFactory alloc] init]; 2.获取ICESevers 3.创建连接对象 RTCPeerConnection *connection = [_factory peerConnectionWithConfiguration:configuration constraints:[self creatPeerConnectionConstraint] delegate:self]; 4.创建datachannel RTCDataChannel *channel = [connection dataChannelForLabel:@"sendDataChannel" configuration:dataChannelConfiguration]; channel.delegate = self; 需要注意的是,创建offer 必须在创建datachannel 之后,因为offer之中包含了一些配置信息,而且两端的一些其他参数要配置一直,不然的话,有可能会通道创建不起来,或者创建起来了,但是数据发送不了。 2. 数据发送 dataChannel 转发的时候,有个缓冲区,这个缓冲区大概是15MB, 也就是你往这个通道你们发数据,缓冲区大小超过这个数据的话

【Spring】- IOC容器初始化过程

女生的网名这么多〃 提交于 2019-11-29 05:19:52
IOC容器案例:XmlBeanFactory XmlBeanFactory容器初始化过程 : XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("com/zhiwei/ioc/applicationContext.xml")); 步骤1 :XmlBeanFactory 使用 ClassPathResource 映射Spring XML配置文件 new ClassPathResource("com/zhiwei/ioc/applicationContext.xml") 步骤2 :初始化空白的IOC容器,然后使用XmlBeanDefinitionReader解析Spring XML配置文件,并注入到IOC容器 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { //传递父BeanFactory初始化父类 super(parentBeanFactory); //BeanDefinition解析 this.reader.loadBeanDefinitions(resource); } 步骤3 : 查看XmlBeanDefinitionReader

gridview

社会主义新天地 提交于 2019-11-29 00:22:46
开始时使用flow布局,ajax请求的图片闪烁,后来改用gridview解决问题 /*--> */ /*--> */ GridView { cellWidth: 200 cellHeight: 150 model: contactModel delegate: Column { width: 200 height: 150 Image { width: parent.width horizontalAlignment: Image.AlignHCenter sourceSize.height: 100 fillMode: Image.PreserveAspectFit source: imgurl Text { anchors.bottomMargin: 30 width: parent.width anchors.top: parent.bottom horizontalAlignment: Text.AlignHCenter font.pointSize: 10 text: name } } } } 来源: https://www.cnblogs.com/wang0535/p/11434976.html

Feign httpclient文件上传问题记录

强颜欢笑 提交于 2019-11-28 20:34:35
Feign httpclient文件上传问题记录 问题说明 原先项目http请求通过feign + ribbon + urlconnection 完成,考虑urlconnection频繁连接释放带来网络及cpu开销问题采用http client作为连接池,升级完后发现发现出现调用下游服务乱码、传文件 文件内容被篡改问题 处理 乱码问题 原先系统feign-core、feign-httpclient使用9.5.0 ,按 GitHub issue 看,这个版本并不是使用utf-8作为编码,导致中文乱码情况,鉴于项目用的版本相对较老,这边只升级到9.6.0解决了乱码问题,之前也有通过feign拦截器处理,但是这样需要判断特定的content-type进行处理,避免麻烦,open-feign强调他们的编码是utf-8,部分版本不是,可以定义为bug,既然是bug,能通过升级解决问题就直接升级了 传文件内容被篡改 有问题找GitHub,还是由于版本问题, 老版本的一个bug ,通过httpclient导致文件上传内容篡改,3.0.3版本修复了,早期项目使用的版本是2.0.1,有点年头,参考了下官方建议要求 Requirements The feign-form extension depend on OpenFeign and its concrete versions: all feign

代理

只谈情不闲聊 提交于 2019-11-28 18:42:15
静态代理 public interface Subject { void dealTask(String taskName); } public class RealSubject implements Subject { @Override public void dealTask(String taskName) { System.out.println("Running Task:" + taskName); try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public class ProxySubject implements Subject { private Subject delegate; public ProxySubject(Subject delegate) { this.delegate = delegate; } @Override public void dealTask(String taskName) { long startTime = System.currentTimeMillis(); delegate.dealTask(taskName); long endTime = System