Java中的不可变类(Immutable class in Java)
根据许多文档,我已经看到一个不可变类应该具有以下特征:
上课应该是最后的
所有的方法应该是最终的
所有的变量应该是最终的
不应该有任何制定者
但我的问题是:
如果我有一个只有最终变量的类,该怎么办?
如果我也有setter,我不能改变Object的状态,因为我有所有的最终变量。 那么这将如何影响不变性?
在这种情况下继承如何改变对象状态?
As per many documentations, I have seen that an immutable class should have the following features:
class should be final
all the methods should be final
all the variables should be final
there should not be any setters
But my questions are:
What if I have a class with only final variables?
If I do have setters also, I cannot change the state of the Object as I have all the final variables. So how will this affect immutability?
How can inheritance change the object state in this case?
原文:https://stackoverflow.com/questions/28984664
最满意答案
找到了将对象转换为数组的方法
var nArray = jQuery.makeArray(s);
工作
found the method to convert object to array
var nArray = jQuery.makeArray(s);
worked
相关问答
更多-
离子3 - 想要将对象数组转换为控制器中的字符串数组(Ionic 3 - Want to convert array of object to array of strings in controller)[2022-09-08]
你可以像这样在一行中做到这一点: this.users = this.users.map(user => user.name); You can do that in a single line like this: this.users = this.users.map(user => user.name); -
你这样做是错误的,通过symfony路由发送它: url: "{{ path('save_sort_order') }}", 这应该是你所需要的 You are doing it the wrong way, send it via the symfony routing: url: "{{ path('save_sort_order') }}", this should be all you need
-
找到了将对象转换为数组的方法 var nArray = jQuery.makeArray(s); 工作 found the method to convert object to array var nArray = jQuery.makeArray(s); worked
-
Ajax仅将第一个字符串数组的索引返回给Spring控制器(Ajax returns only first index of stringified array to the Spring controller)[2022-02-16]
Ajax调用是异步的,这意味着它将立即启动它们中的所有3个,调用getPoolsData不会等待get完成,您需要将ajax调用设置为异步。 喜欢这个 $.ajaxSetup({ async: false }); 请注意,这会将所有ajax调用设置为异步,更好的做法是重写您的调用 $.ajax({ url: "...", type: "GET", data: ..., async: false }); 只做这些调用异步 或者你可以使用setInterval来检查是 ... -
一个问题似乎是您在允许的参数中实际上没有:id ,但是您在提供的示例中传递了一个id密钥。 对于句子,尝试将允许的参数中的sentences键映射到空数组: def game_params params.require(:game).permit(:hope, :wisdom, :user_id, :mnemonic, sentences: []) end 此堆栈溢出注释中的更多信息: https : //stackoverflow.com/a/16555975/2909095 One issue a ...
-
使用良好的常规HTTP状态代码来传达API上的操作结果可能是一个好主意。 在创建资源时传递成功的适当代码是201 。 通常,响应主体将包含新创建的资源的ID或URL。 10.2.2 201创建 请求已经完成并且导致创建新的资源。 新创建的资源可以由响应实体中返回的URI(s)引用,其中由Location标题字段给出的资源的URI最具体。 响应应该包括一个实体,其中包含资源特性和位置的列表,用户或用户代理可以从中选择最合适的资源。 实体格式由Content-Type头字段中给出的媒体类型指定。 源服务器必须在 ...
-
问题是你发送矛盾的信息到你的服务器: ContentType: "application/json" RequestBody: "id=10" // Not JSON 如果你不需要发送json,一种解决方案是简单地从你的$.ajax调用中移除contentType选项,因为jQuery将它设置为application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8默认applicat ...
-
我建议两件事 1)更换 data: JSON.stringify({ "keys": [ "1", "2" ] }), 通过 data: JSON.stringify([ "1", "2" ] ), // <-- Send the array directly 这样做你将传递一个数组作为参数,而不是一个包含名为keys的数组的对象 2)如果以前的建议不起作用,请更换 ActivateKeys(List
keys) 通过 ActivateKeys(IList keys) ... -
我自己已经完成了。 在JavaScript中我首先将我的文件转换为base64,然后使用javax.xml.bind.DatatypeConverter.parseBase64Binary()将其转换为spring控制器中的byte []。 以下是手动将文件转换为base64的JavaScript代码: $scope.onFileSelect = function(ele){ $scope.files = ele.files; var reader = new FileReader(); ...
-
Stringify to JSON ,添加dataType: 'json' ,然后传递并更正你的"" var postData = JSON.stringify({ deleteArr: deleteArr }); if(deleteArr.length > 0) { $.ajax({ url: @Url.Action("Delete", "ASZ01"), type: "POST", data: postDa ...