- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在阅读“Java concurrency in practice”,起初,它说:
An object's state starts with its fields. If they are all of primitive type, the fields comprise the entire state.
If the object has fields that are references to other objects, its state will encompass fields from the referenced objects as well.
然后它说:
an object's state could be a subset of the fields in the object graph rooted at that object. Why might it be a subset? Under what conditions are fields reachable from a given object not part of that object's state?
我在书中找不到两个问题的答案。
而且我对上面的两个引用完全感到困惑。这对我来说似乎是矛盾的。有人可以举一个例子来说明“对象的状态是对象图中以该对象为根的字段的子集”并回答这两个问题吗?
最佳答案
从评论来看,您的困惑似乎是:
I think the meaning is "all fields reachable from am object are part of the object's state". Isn't it?
不,不是这个意思。作者提出的问题表达了这一点:
Under what conditions are fields reachable from a given object not part of that object's state?
很快就得到了回答:
Collection classes often exhibit a form of “split ownership”, in which the collection owns the state of the collection infrastructure, but client code owns the objects stored in the collection
为清楚起见,所有权决定了谁可以为该状态实现同步策略。
In many cases, ownership and encapsulation go together—the object encapsulates the state it owns and owns the state it encapsulates. It is the owner of a given state variable that gets to decide on the locking protocol used to maintain the integrity of that variable’s state
当状态被封装时,客户端被迫通过任何封装状态与状态交互。因此,封装者是独占者,线程安全由独占者决定。
once you publish a reference to a mutable object, you no longer have exclusive control; at best, you might have “shared ownership”.
如果对象被公开(可能通过 getter),封装者将失去独占所有权,因为客户端现在可以绕过封装者设置的任何策略。
但是,这不会改变对象仍然部分由封装器拥有的事实 - 当客户端通过封装器修改对象的状态时,封装器可能仍会强制执行它的策略。
Why may it be a subset?
摘自本书:
If you allocate and populate a
HashMap
, you are creating multiple objects: theHashMap
object, a number ofMap.Entry
objects used by the implementation ofHashMap
, and perhaps other internal objects as well.
HashMap
可能由许多 Map.Entry
对象组成,形式为 HashMap#Node
.
由于 HashMap
控制了客户端如何与 Node
交互(客户端可能不会创建新的 Node
实例,只能通过 HashMap#putVal
决定状态如Node#hash
),Node
的状态被认为是HashMap
的子集>.
Under what conditions are fields reachable from a given object not part of that object's state?
摘自本书:
Collection classes often exhibit a form of “split ownership”, in which the collection owns the state of the collection infrastructure, but client code owns the objects stored in the collection.
Servlets need not use synchronization when calling set- Attribute and getAttribute, but they may have to use synchronization when using the objects stored in the ServletContext
集合无法控制其元素的线程安全 - 客户端在使用从集合接收的元素时可能必须实现自己的同步。
由于集合无法控制其元素的线程安全性,因此它不拥有其元素的状态;元素的状态不是集合状态的子集,只有基础设施是。
关于java - 为什么对象的状态可以是对象图中以该对象为根的字段的子集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665583/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!