不可变类的示例 [英] Examples of immutable classes

查看:71
本文介绍了不可变类的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道了不可变类的定义,但我需要一些例子。

I already know the definition of immutable classes but I need a few examples.

推荐答案

标准API中的一些着名的不可变类:

Some famous immutable classes in the Standard API:


  • java.lang.String(已经提到过)

  • 基本类型的包装类:java.lang.Integer,java.lang.Byte,java.lang。 Character,java.lang.Short,java.lang.Boolean,java.lang.Long,java.lang.Double,java.lang.Float

  • java.lang.StackTraceElement(用于构建异常堆栈跟踪)

  • 大多数枚举类都是不可变的,但这实际上取决于具体情况。 (不要实现可变的枚举,这会使你搞砸了。)我认为至少标准API中的所有枚举类实际上都是不可变的。

  • java.lang.String (already mentioned)
  • The wrapper classes for the primitive types: java.lang.Integer, java.lang.Byte, java.lang.Character, java.lang.Short, java.lang.Boolean, java.lang.Long, java.lang.Double, java.lang.Float
  • java.lang.StackTraceElement (used in building exception stacktraces)
  • Most enum classes are immutable, but this in fact depends on the concrete case. (Don't implement mutable enums, this will screw you up somewhen.) I think that at least all enum classes in the standard API are in fact immutable.

java.math.BigInteger和java.math.BigDecimal(至少这些类本身的对象,子类可能会引入可变性,虽然这不是一个好主意)

java.math.BigInteger and java.math.BigDecimal (at least objects of those classes themselves, subclasses could introduce mutability, though this is not a good idea)

的java.io.File。请注意,这表示VM外部的对象(本地系统上的文件),可能存在也可能不存在,并且有一些方法可以修改和查询此外部对象的状态。但File对象本身保持不变。 (java.io中的所有其他类都是可变的。)

java.io.File. Note that this represents an object external to the VM (a file on the local system), which may or may not exist, and has some methods modifying and querying the state of this external object. But the File object itself stays immutable. (All other classes in java.io are mutable.)

java.awt.Font - 表示在屏幕上绘制文本的字体(可能有一些可变子类,但这肯定没用。)

java.awt.Font - representing a font for drawing text on the screen (there may be some mutable subclasses, but this would certainly not be useful)


  • java.awt.GradientPaint,

  • java.awt.LinearGradientPaint

  • java.awt。 RadialGradientPaint,

  • (我不确定java.awt.TexturePaint)

  • java.awt.GradientPaint,
  • java.awt.LinearGradientPaint
  • java.awt.RadialGradientPaint,
  • (I'm not sure about java.awt.TexturePaint)

java.awt.Cursor - 表示鼠标光标的位图(这里也有一些子类可能是可变的或取决于外部因素)

java.awt.Cursor - representing the bitmap for the mouse cursor (here too, some subclasses may be mutable or depending on outer factors)

java。 util.Locale - 代表特定的地理,政治或文化区域

java.util.Locale - representing a specific geographical, political, or cultural region.

虽然大多数集合都是可变的,但也有一些java.util.Collections类中的包装器方法,它返回集合上的不可修改的视图。如果你传递了一个在任何地方都不知道的集合,这些集合实际上是不可变的集合。此外, Collections.singletonMap() .singletonList .singleton 返回不可变的单元素集合,并且还有不可变的空元素。

while most collections are mutable, there are some wrapper methods in the java.util.Collections class, which return an unmodifiable view on a collection. If you pass them a collection not known anywhere, these are in fact immutable collections. Additionally, Collections.singletonMap(), .singletonList, .singleton return immutable one-element collections, and there are also immutable empty ones.

java.net.URL和java.net.URI - 表示资源(在互联网或其他地方)

java.net.URL and java.net.URI - representing a resource (on the internet or somewhere else)

可以说原始类型也是不可变的 - 你不能改变42的值,可以吗?

One could say the primitive types are immutable, too - you can't change the value of 42, can you?


是类AccessControlContext一个不可变类

is Class AccessControlContext a immutable class

AccessControlContext没有任何变异方法。它的状态包含一个ProtectionDomains列表(它是一个不可变类)和一个DomainCombiner。 DomainCombiner是一个接口,因此原则上实现可以在每次调用时执行不同的操作。

AccessControlContext does not have any mutating methods. And its state consists of a list of ProtectionDomains (which is an immutable class) and a DomainCombiner. DomainCombiner is an interface, so in principle the implementation could do something different on each call.

事实上,ProtectionDomain的行为也可能取决于当前有效的策略 - 是否要将这样的对象称为不可变是有争议的。

In fact, also the behaviour of the ProtectionDomain could depend on the current policy in force - it is disputable whether to call such an object immutable.


和AccessController?

and AccessController?

没有AccessController类型的对象,因为这是一个没有可访问构造函数的final类。所有方法都是静态的。可以说AccessController既不可变也不可变,或者两者兼而有。

There are no objects of type AccessController, since this is a final class with no accessible constructor. All methods are static. One could say AccessController is neither mutable nor immutable, or both.

同样适用于所有其他没有对象(实例)的类,最着名的是:

The same is valid for all other classes which can't have objects (instances), most famously:


  • java.lang.Void

  • java.lang.System(但这有一些可变的静态 - out 错误

  • java.lang.Math(这也是 - 随机数生成器)

  • java.lang.reflect.Array

  • java.util.Collections

  • java.util.Arrays

  • java.lang.Void
  • java.lang.System (but this has some mutable static state - in, out, err)
  • java.lang.Math (this too - the random number generator)
  • java.lang.reflect.Array
  • java.util.Collections
  • java.util.Arrays

这篇关于不可变类的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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