Java “实现包装方法”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1736234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is meant by "implement a wrapper method"?
提问by Shawn
I have been given a programming assignment and one of the things I have to do is implement method which a wrapper method which relies on another method to sort the coordinates from lowest to highest. I am unsure on what exactly is meant by implementing a wrapper method.
我得到了一个编程任务,我必须做的一件事是实现方法,该方法依赖于另一种方法来从最低到最高对坐标进行排序。我不确定实现包装器方法的确切含义。
static void sortCoordsByZ(double[][] coords) {
//implement the wrapper method for the recursive sort method. all work is done the recursive sort method
}
static void recursiveSort(double[][] coords, int lo, int hi) {
//recursive sort method
}
回答by Dmitry
It wraps another method :) Probably it adds some additional arguments, like initial values of lo and hi in your case and acts as an entry point to your sorting.
它包装了另一种方法:) 可能它添加了一些额外的参数,比如在你的情况下 lo 和 hi 的初始值,并作为你排序的入口点。
回答by Jeff Sternal
回答by slugster
When you implement a wrapper method, you are effectively coding up a variant of an existing method, usually because the existing method doesn't satisfy your current requirements. The original method may be too complicated (too many parameters), or it may not quite do the required thing, which means you have to write a wrapper (or overload) that does the extra work the original method doesn't. Usually when writing a wrapper you will still leverage the original function for whatever it does, and fill in the gaps with your wrapper.
当您实现包装方法时,您实际上是在对现有方法的变体进行编码,这通常是因为现有方法不能满足您当前的要求。原始方法可能太复杂(参数太多),或者它可能无法完成所需的工作,这意味着您必须编写一个包装器(或重载)来完成原始方法没有的额外工作。通常在编写包装器时,您仍然会利用原始函数执行任何操作,并用包装器填补空白。
回答by Grandpa
You're acting as a wrapper method right now by asking your assignment question on Stack Overflow!
您现在通过在 Stack Overflow 上询问您的分配问题来充当包装方法!
A wrapper method answers a question by asking an "expert" method for the answer. Generally, it does three things:
包装方法通过向“专家”方法询问答案来回答问题。一般来说,它会做三件事:
- It frames the question in such a way that the "expert" method can understand it.
- It asks the "expert" method the question
- It does something easy to the answer to put it in the right format for the caller.
- 它以“专家”方法可以理解的方式构建问题。
- 它向“专家”方法提出问题
- 为呼叫者以正确的格式提供答案很容易。
In your case, the "expert" method is recursiveSort()
, and your sortCoordsByZ()
method will have to call recursiveSort()
with the right parameters, and then maybe do something with the answer before returning it.
在您的情况下,“专家”方法是recursiveSort()
,您的sortCoordsByZ()
方法必须recursiveSort()
使用正确的参数进行调用,然后可能在返回答案之前对其进行处理。
回答by Shawn
In that case shouldn't this be all I need to put in the "sort by z" method if its going through an array called coords that has all its slots filled?
在这种情况下,如果它通过一个名为 coords 的数组,它的所有插槽都已填充,那么这不应该是我需要放入“按 z 排序”方法的全部内容吗?
recursiveSort(coords, 0, coords.length-1);
回答by Ryan Holsman
Wrapper Methods can be used for Abstraction, Standardization, and Refactoring
包装方法可用于抽象、标准化和重构
For example if you import a library of special helper methods and use them through out your application, then later decide to switch to a different library that will give you more functionality you would then have to rewrite every line of code that calls the previous libraries methods to call the new libraries methods. With application reaching tens and hundreds of thousands of lines this is a huge task. This happens more frequently when software is licensed and then when it expires a new option is picked.
例如,如果您导入一个特殊辅助方法库并在整个应用程序中使用它们,然后决定切换到一个不同的库,这将为您提供更多功能,然后您将不得不重写调用先前库方法的每一行代码调用新的库方法。随着应用程序达到数万行,这是一项艰巨的任务。当软件获得许可时,这种情况会更频繁地发生,然后当它到期时会选择一个新选项。
To solve this problem you can wrap all of the Helper methods in your own methods. These methods simply be 1 line that calls the Methods in your imported library. You would call all of the wrapper methods through out your application instead of directly calling the imported library.
要解决此问题,您可以将所有 Helper 方法包装在您自己的方法中。这些方法只是调用导入库中的方法的 1 行。您将在整个应用程序中调用所有包装器方法,而不是直接调用导入的库。
Now the benefit of all the abstraction comes in when you want to import the new library. Instead of rewriting every call through out the application you can simply just rewrite the wrapper methods which should be its own class. Now changing 1 method in the wrapper class updates the entire application where that method is used. (See MVC4 .NET HTML Helper methods for an example)
现在,当您想要导入新库时,所有抽象的好处都会出现。您可以简单地重写应该是它自己的类的包装器方法,而不是在整个应用程序中重写每个调用。现在更改包装类中的 1 个方法会更新使用该方法的整个应用程序。(有关示例,请参阅 MVC4 .NET HTML Helper 方法)
On the subject of standardization the wrappers can be used to set default values for many of the things that are set manually when the object is created. But now if they are set in the wrapper they no longer must be set every time the original method is called. For example you could set the dimensions of a Grid View in the Gridview wrapper and now when you call the wrapper method in your application all grid views have the same dimensions and fewer lines of code need to be maintained throughout the application; also accomplishing refactoring.
在标准化方面,包装器可用于为创建对象时手动设置的许多内容设置默认值。但是现在如果它们是在包装器中设置的,则不再需要在每次调用原始方法时进行设置。例如,您可以在 Gridview 包装器中设置网格视图的尺寸,现在当您在应用程序中调用包装器方法时,所有网格视图都具有相同的尺寸,并且在整个应用程序中需要维护的代码行更少;还完成了重构。