什么是包装器类? 这样的类有什么用处呢?
当前回答
Java编程为每个基元数据类型提供包装类,用于将基元数据类型转换为包装类的对应对象。
其他回答
包装器类是唯一用于保存某些东西并向其添加一些功能的类。 在Java中,由于原语(如int,float,char…)不是对象,所以如果你想把它们当作一个对象,那么你必须使用包装类。 假设你想创建一个int型的Vector,问题是Vector只保存对象而不是原语。所以你要做的就是把所有的整型放在一个Integer包装器中并使用它。例子:
int number = 5;
Integer numberWrapped = new Integer(number);
//now you have the int in an object.
//and this is how to access the int value that is being wrapped.
int again = numberWrapped.intValue();
包装器类是一个包装另一个类并提供客户端和被包装的原始类之间的抽象的类。
我目前在我的项目中使用了一个包装器类,我得到的主要好处(只是一个扩大主题解释的好处):
异常处理:我的主类(另一个类包装的类)具有在发生异常时抛出异常的方法,因此我创建了一个包装类来处理异常并立即记录它们。因此,在我的主作用域中,没有异常处理。我只是调用一个方法并做一些事情。
易于使用:我可以很容易地启动对象。通常起始阶段由很多步骤组成。
代码可读性:当另一个程序员打开我的代码时,代码看起来非常清晰,易于操作。
隐藏细节:如果你正在生成另一个程序员将要使用的类,那么你可以包装细节,如“错误处理,异常处理,日志消息等……”,这样程序员就不必处理混乱,只需要简单地使用方法。
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.
包装器是用于在不同平台之间的两个不同应用程序之间进行通信的类
推荐文章
- 在PHP5中创建单例设计模式
- 什么时候我们应该使用观察者和可观察对象?
- 在哪里放置AutoMapper.CreateMaps?
- 使用Enum实现单例(Java)
- 由Jon Skeet撰写的《Singleton》澄清
- 为什么c#不提供c++风格的'friend'关键字?
- 什么是包装器类?
- MVC, MVP和MVVM设计模式在编码c#方面有什么不同
- 设计模式:工厂vs工厂方法vs抽象工厂
- 用MVVM处理WPF中的对话框
- 战略设计模式与国家设计模式的区别是什么?
- 什么是反模式?
- 让setter返回"this"是不好的做法吗?
- 在c++中是否有与typedef关键字等价的Java或方法论?
- Android上使用哪些架构模式?