如何将重载的功能传递给另一个类? [英] How do I pass overloaded funtions to another class?

查看:79
本文介绍了如何将重载的功能传递给另一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种UpdateStatusLine:

I have two variations of UpdateStatusLine:

public delegate void DelegateUpdateStatusLine(String text);
private DelegateUpdateStatusLine m_DelegateUpdateStatusLine;
public delegate void DelegateUpdateStatusLine(String text, int percentage);

private void UpdateStatusLine(String text)
	{ ... }
private void UpdateStatusLine(String text, int percentage)
	{ ... }



我需要将委托传递给另一个将使用这两个重载的类.传递任何一个都很简单.



I need to pass the delegate to another class that will use both overloads. Passing either one is simple.

m_DelegateUpdateStatusLine = new DelegateUpdateStatusLine(this.UpdateStatusLine);
AnotherClass x = new AnotherClass(m_DelegateUpdateStatusLine);



如何传递允许AnotherClass使用UpdateStatusLine的任何一种形式的委托?



How do I pass delegates that allow AnotherClass to use either form of UpdateStatusLine?

推荐答案

您可以在同一个类中同时使用两个重载方法.对此没有限制.
您可以简单地拨打您想要的电话!
You can have both the overloaded methods in the same class. There is no restriction on that.
You can they simply call just the one you want!


嘿,

委托在某种程度上类似于C中的Pointer.它们所做的只是指向某事物,并且不包含逻辑.因此,它不能一次指向两个事物并假设可以,因为它没有逻辑,因此无法找出要调用的那个.

根据我的观点,有2种方法:
1.使用默认参数创建重载方法,然后在委托中传递该方法.您必须编写逻辑以查看是否分配了某个参数,或者该参数保持默认值.
2.您已经找到了一种方法的实现方法.为什么不同时对另一个重载并以相同的方式传递呢? IE.您将有2个参数.

希望对您有所帮助
Hey there,

Delegates are somewhat similar to Pointers in C. All they does is point at something and they don''t contain logic. Therefore, it cannot point at 2 things at once and assuming it can, since it got no logic it wouldn''t be able to figure out which one to call.

There are 2 approaches based on my opinion:
1. To create the overloaded method with default parameters and pass that method in the delegate. You have to write logic to see whether a certain parameter is assigned or it stay the default value.
2. You''ve figured out the way to do it for one method. Why not do it to the other overload as well and pass the same way? I.e. you''d have 2 parameters.

Hope this helps, regards


这篇关于如何将重载的功能传递给另一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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