问题描述
什么是包装班?
这些课程如何有用?
推荐答案
通常,包装器类是"包装"或"封装"另一类或组件功能的任何类.通过从基础类或组件的实现中提供一定的抽象来有用;例如,包装COM组件的包装类可以管理调用COM组件的过程,而无需使用它来调用调用代码.他们还可以通过减少涉及的数字接口点来简化基础对象的使用;通常,这使得更安全地利用了基础组件.
其他推荐答案
听起来的样子:一个"包装"另一个类或API功能的类,在更简单或仅不同的API中.
请参阅: adapter pattern ,外观模式
其他推荐答案
包装类别提供一种将原始类型用作对象的方法.对于每个原始性,我们都有一个包装类别,例如,
int Integer byte Byte
整数和字节是原始int和字节的包装类别.当您需要将原始词用作对象时,有时间/限制,因此包装器类提供一种称为拳击/拆箱的机制.
概念可以通过以下示例很好地理解为
double d = 135.0 d; Double doubleWrapper = new Double(d); int integerValue = doubleWrapper.intValue(); byte byteValue = doubleWrapper.byteValue(); string stringValue = doubleWrapper.stringValue();
因此,这就是方式,我们可以使用包装类类型也可以转换为其他原始类型.当您需要将原始类型转换为对象并使用它们以获取其他原语时,使用这种类型的转换.尽管如此,您需要编写一个大型代码.但是,通过简单的铸造技术可以实现同样的方法,因为可以在下面的情况下实现代码段.
double d = 135.0; int integerValue = (int) d ;
尽管双重值明确转换为整数值也称为降落.
问题描述
What is a wrapper class?
How are such classes useful?
推荐答案
In general, a wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. These are useful by providing a level of abstraction from the implementation of the underlying class or component; for example, wrapper classes that wrap COM components can manage the process of invoking the COM component without bothering the calling code with it. They can also simplify the use of the underlying object by reducing the number interface points involved; frequently, this makes for more secure use of underlying components.
其他推荐答案
Just what it sounds like: a class that "wraps" the functionality of another class or API in a simpler or merely different API.
See: Adapter pattern, Facade pattern
其他推荐答案
Wrapper classes provide a way to use primitive types as objects. For each primitive , we have a wrapper class such as,
int Integer byte Byte
Integer and Byte are the wrapper classes of primitive int and byte. There are times/restrictions when you need to use the primitives as objects so wrapper classes provide a mechanism called as boxing/unboxing.
Concept can be well understood by the following example as
double d = 135.0 d; Double doubleWrapper = new Double(d); int integerValue = doubleWrapper.intValue(); byte byteValue = doubleWrapper.byteValue(); string stringValue = doubleWrapper.stringValue();
so this is the way , we can use wrapper class type to convert into other primitive types as well. This type of conversion is used when you need to convert a primitive type to object and use them to get other primitives as well.Though for this approach , you need to write a big code . However, the same can be achieved with the simple casting technique as code snippet can be achieved as below
double d = 135.0; int integerValue = (int) d ;
Though double value is explicitly converted to integer value also called as downcasting.