术语“上下文"指的是“上下文".在编程中? [英] The term "Context" in programming?

查看:276
本文介绍了术语“上下文"指的是“上下文".在编程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编程了几个月了,在课堂上一个经常使用的词是"context".像ServletContext(Java),Activity(Android),Service(Java,Android),NSManagedContext(Objective-C,iOS)一样.

通过查看字典,我看到这个词的意思是:情况,环境,情况等.但是,由于我不是英语为母语的人,所以我不明白我应该将其直接翻译成什么.例如,如果我要编写一个名为SomeClassContext的类,或者编写一个具有上下文参数的方法,那么我将无法理解何时应为其命名上下文,因为我不理解它.

我一直在搜索Stack Overflow上的上下文,但是没有任何问题/答案可以帮助我.

如果有人可以向我提供解释,我将非常高兴.

解决方案

假设您去看牙医拔牙.

当接待员询问您的姓名时,这就是他们开始约会所需要的信息.在此示例中,您的姓名为 上下文信息 .因此,在拜访牙医的 上下文 中,您需要提供姓名以拔出牙齿.

现在,假设您走进银行.

在银行,您要求提取$ 100.出纳员需要先确定您的身份,然后才能给您钱,因此您可能必须向他们出示驾驶执照或刷ATM卡并输入PIN码.无论哪种方式,您提供的都是上下文.柜员使用此信息来推动交易前进.然后,他们可能会问您您要从哪个帐户中提款.当您回答我的储蓄帐户" 时,这甚至是更多背景信息.

您提供的上下文越多,另一方就需要更多的知识来帮助处理您的请求.有时上下文是可选的(例如,在您的Google搜索中输入越来越多的单词以获得更好的结果),有时则是必需的(例如,在ATM上提供您的PIN码).无论哪种方式,通常都是有助于完成工作的信息.

现在,假设您拿了100美元买了一张机票,在嘴巴愈合的时候在温暖的地方飞行.

您到达了一个阳光明媚的目的地,但您的皮包却没有成功.它在机场系统的某个地方丢失了.因此,您将行李领取票"(带有条形码的不干胶标签)带到行李寄存处".桌子后面的人首先要问的是那张票上有您的行李号码.这是一些必需上下文的示例.

但是随后行李员要求您提供有关行李的更多信息 ,这样他们可以更轻松地找到它.他们问:"它是什么颜色?它是什么尺寸?它有轮子吗?是硬的还是软的?虽然他们不一定需要这些信息,但如果需要的话,它可以缩小信息范围.您提供它们.它减少了问题区域,使搜索更快.这是可选上下文.

这是有趣的部分:对于许多软件和API,所需的上下文通常以方法签名中的实际参数结尾,而可选上下文则移至其他地方,例如可以包含任何内容(可以为空)或进入线程本地存储的灵活键/值映射,可以在需要时对其进行访问.

以上示例来自现实生活,但是您可以轻松地将它们映射到计算机科学领域.例如,HTTP标头包含上下文信息.每个标头都与发出请求的信息有关.或者,当您在两阶段提交过程的一部分中发送全局交易ID时,该交易ID是上下文.它可以帮助事务经理协调工作,因为它是有关手头全部任务的信息.

希望有帮助.

I have been programming for some months now and a frequently used word is "context" in classes. Like ServletContext (Java), Activity (Android), Service (Java, Android), NSManagedContext (Objective-C, iOS).

By looking in dictionaries I see that the word means: situation, environment, circumstances etc. However, because I am not a native English speaker I do not understand what I should translate it directly to. For instance, if I were to write a class that either was named SomeClassContext, or a method that had a context parameter, I would not understand when I should name it context because I do not understand it.

I have been searching for context on Stack Overflow, but no question/answers was able to help me.

I would be very happy if someone could provide me with the explanation.

解决方案

Let's say you go to the dentist to have a tooth pulled out.

When the receptionist asks you for your name, that's information they need in order to begin the appointment. In this example, your name is contextual information. So in the context of visiting the dentist, you need to provide your name to get your tooth pulled.

Now let's say you walk over to the bank.

At the bank, you ask to withdraw $100. The teller needs to establish your identity before giving you money, so you'll probably have to show them a driver's license or swipe your ATM card and enter your PIN number. Either way, what you're providing is context. The teller uses this information to move the transaction forward. They may then ask you which account you'd like to withdraw from. When you answer, "My savings account", that's even more context.

The more context you give, the more knowledge the other party has to help deal with your request. Sometimes context is optional (like typing more and more words into your Google search to get better results) and sometimes it's required (like providing your PIN number at the ATM). Either way, it's information that usually helps to get stuff done.

Now let's say you take your $100 and buy a plane ticket to fly somewhere warm while your mouth heals.

You arrive at a nice sunny destination, but your bag doesn't make it. It's lost somewhere in the airport system. So, you take your "baggage claim ticket" (that sticker with the barcode on it) to the "Lost Baggage office". The first thing the person behind the desk will ask for is that ticket with your baggage number on it. That's an example of some required context.

But then the baggage person asks you for more information about your bag like so they can find it more easily. They ask, "What color is it? What size is it? Does it have wheels? Is it hard or soft? While they don't necessarily need those pieces of information, it helps narrow things down if you provide them. It reduces the problem area. It makes the search much faster. That's optional context.

Here's the interesting part: for a lot of software and APIs, the required context usually ends up as actual parameters in a method signature, and optional context goes somewhere else, like a flexible key-value map that can contain anything (and may be empty) or into thread-local storage where it can be accessed if needed.

The examples above are from real life, but you can easily map them to areas within computer science. For example, HTTP headers contain contextual information. Each header relates to information about the request being made. Or when you're sending along a global transaction ID as part of a two-phase commit process, that transaction ID is context. It helps the transaction manager coordinate the work because it's information about the overall task at hand.

Hope that helps.

这篇关于术语“上下文"指的是“上下文".在编程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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