如何解释方法调用?
让我们考虑一个小方法:
int MyFunction(string foo, int bar)
{
...
}
和一些调用:
MyFunction("",0)
int x = MyFunction(foo1,bar1)
您如何向非技术人员解释这一点?有人有一个很好的比喻吗?
我多次尝试解释方法调用(或函数应用),但都失败了。在这里我似乎找不到合适的词语。
问候, forki
更新:对我来说解释参数如何传递/匹配很重要。
let's consider a small method:
int MyFunction(string foo, int bar)
{
...
}
and some calls:
MyFunction("",0)
int x = MyFunction(foo1,bar1)
How would you explain this to a non-technical persons? Has anybody a nice metaphor?
I tried to explain method calling (or function application) several times, but I failed. Seems I can't find the right words here.
Regards,
forki
UPDATE: It is important for me to explain how the parameters are passed / matched.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
(高度非技术性的解决方案)
这就像下订单:
(Highly non-technical solution)
It's like making an order:
您可以看出函数是一个可以被其他对象调用的对象的过程。可以说“你”是一个具有“工作”功能的对象。您的“老板”是调用者对象。然后,您的老板可以叫您使用不同的类型(即参数)。
最后,你的“老板”可以要求“你”工作(“对此进行编码”)或工作(“检查电子邮件”)或工作(“完成截止日期”)等。
You could tell function is a process available into an object that could be called by other. Lets say "You" is an object with function "Work". Your "Boss" is the caller object. Your Boss then can call you to Work with different type (which is parameter).
In the end Your "Boss" can ask "You" to Work("encode this") or Work("check email") or Work("finish deadline"), etc.
委派任务怎么样?想象一下,你正在烤蛋糕,但面粉用完了。您无需自己购买面粉,只需向您的孩子发送购买面粉的说明即可。输入:金钱,输出:面粉。
How about delegating a task? Imagine you’re baking a cake and ran out of flour. Instead of buying some yourself you could just send your kid with instructions to buy flour. Input: money, output: flour.
如果你不先了解
,就很难理解“方法调用”的概念
控制流程。
一个简单的解释是,方法或例程是打包指令的构造
为了重用它们并使代码更具可读性。
暂时调用一个方法,将执行流程切换到该方法。
It's difficult to understand the "method call" concept if you don't understand first the
flow of control.
A simple explanation is that methods, or routines, is a construct for packeting instructions
in order to reuse them and make the code more readable.
Calling a method, temporarily, switches the execution flow to that method.
您正在告诉
C
在给定条件a
和b
的情况下do
某事。You are telling
C
todo
something , given the conditiona
andb
.最好的方法可能是提出一个特定领域的示例,您正在向其解释的人可以与该示例相关。如果她在邮局工作,您应该描述“将包含此文本的信件发送给此收件人”功能,其中收件人是一个参数(包含地址),消息是文本内容的参数。
只要每个参数都有一个名称,参数顺序并不重要。试图解释为什么顺序在某些神秘的编程语言中很重要是徒劳的。
The best approach is probably to come up with a domain specific example which the person you are explaining to can relate to. If she is working with the post office, you should describe the function "send letter with this text to this recipient", where recipient is a parameter (containing the address) and message is the parameter for the textual content.
Parameter order is not important as long as you have a name for each parameter. Trying to explain why order is important in some arcane programming language is fruitless.
调用
How about
将该系统视为办公桌前的出纳员。要调用某个功能,您需要填写一张表格,要求系统执行某项操作,然后将其交给出纳员。他们去做工作,然后递给你一张写有结果的纸。根据您希望系统执行的操作,您可以选择合适的形式。
MyMethod 的形式表示:
这个类比可以以各种方式扩展。如果表单告诉您 String 和 int 的用途,不是很方便吗?这就是带有命名参数的语言的用武之地。
对于面向对象来说,整个系统不是只有一张桌子,而是每个对象都是自己的出纳员,你向他们提供了一份表格,为了完成工作,他们提供了更多的东西彼此之间来回形成。 ETC。
Think of the system as a teller at a desk. To call a function you fill in a form to ask the system to do something, hand it to the teller. They go off and do the work, then hand you back a piece of paper with the result written on it. Depending on what you want the system to do, you pick an appropriate form.
The form for MyMethod says:
This analogy can be extended in all kinds of ways. Wouldn't it be handy if the form told you what the purpose of the String and int were? That's where languages with named parameters come in.
For OO, instead of having one desk for the whole system, each object is its own teller, you hand a form to them, and in order to get the job done, they hand a lot more forms back and forth between each other. Etc.