Java中的不可变类(Immutable class in Java)

根据许多文档,我已经看到一个不可变类应该具有以下特征:

  1. 上课应该是最后的

  2. 所有的方法应该是最终的

  3. 所有的变量应该是最终的

  4. 不应该有任何制定者

但我的问题是:

  1. 如果我有一个只有最终变量的类,该怎么办?

  2. 如果我也有setter,我不能改变Object的状态,因为我有所有的最终变量。 那么这将如何影响不变性?

  3. 在这种情况下继承如何改变对象状态?


As per many documentations, I have seen that an immutable class should have the following features:

  1. class should be final

  2. all the methods should be final

  3. all the variables should be final

  4. there should not be any setters

But my questions are:

  1. What if I have a class with only final variables?

  2. 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?

  3. How can inheritance change the object state in this case?


原文:https://stackoverflow.com/questions/28984664
2024-05-06 19:05

满意答案

找到了将对象转换为数组的方法

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)

你可以像这样在一行中做到这一点: 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);

如何使用ajax发布到Symfony2中的安全控制器(how to post with ajax to secured controller in Symfony2)

你这样做是错误的,通过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

ajax将一串字符串发布到控制器(ajax post an array of strings to controller)

找到了将对象转换为数组的方法 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)

Ajax调用是异步的,这意味着它将立即启动它们中的所有3个,调用getPoolsData不会等待get完成,您需要将ajax调用设置为异步。 喜欢这个 $.ajaxSetup({ async: false }); 请注意,这会将所有ajax调用设置为异步,更好的做法是重写您的调用 $.ajax({ url: "...", type: "GET", data: ..., async: false }); 只做这些调用异步 或者你可以使用setInterva...

使用AJAX更新Rails列,该列需要字符串数组(Update Rails column that expects array of strings using AJAX)

一个问题似乎是您在允许的参数中实际上没有:id ,但是您在提供的示例中传递了一个id密钥。 对于句子,尝试将允许的参数中的sentences键映射到空数组: def game_params params.require(:game).permit(:hope, :wisdom, :user_id, :mnemonic, sentences: []) end 此堆栈溢出注释中的更多信息: https : //stackoverflow.com/a/16555975/2909095 One is...

MVC控制器的替代方法返回字符串的操作?(Alternatives to MVC Controller Actions returning strings?)

使用良好的常规HTTP状态代码来传达API上的操作结果可能是一个好主意。 在创建资源时传递成功的适当代码是201 。 通常,响应主体将包含新创建的资源的ID或URL。 10.2.2 201创建 请求已经完成并且导致创建新的资源。 新创建的资源可以由响应实体中返回的URI(s)引用,其中由Location标题字段给出的资源的URI最具体。 响应应该包括一个实体,其中包含资源特性和位置的列表,用户或用户代理可以从中选择最合适的资源。 实体格式由Content-Type头字段中给出的媒体类型指定。 源服...

为什么发布数据完全空了通过ajax发布到CodeIgniter控制器?(Why is post data completely empty posting to CodeIgniter controller via ajax?)

问题是你发送矛盾的信息到你的服务器: 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默认app...

将带有字符串的json数组发布到Asp.Net MVC操作控制器始终为null(Posting json array with strings to Asp.Net MVC action controller is always null)

我建议两件事 1)更换 data: JSON.stringify({ "keys": [ "1", "2" ] }), 通过 data: JSON.stringify([ "1", "2" ] ), // <-- Send the array directly 这样做你将传递一个数组作为参数,而不是一个包含名为keys的数组的对象 2)如果以前的建议不起作用,请更换 ActivateKeys(List<string> keys) 通过 ActivateKeys(IList<string> k...

Post方法一个文件和字符串请求到弹簧休息控制器接收字节[]和字符串使用角?(Post method a file and string request to spring rest controller that receive byte[] and Strings using angular?)

我自己已经完成了。 在JavaScript中我首先将我的文件转换为base64,然后使用javax.xml.bind.DatatypeConverter.parseBase64Binary()将其转换为spring控制器中的byte []。 以下是手动将文件转换为base64的JavaScript代码: $scope.onFileSelect = function(ele){ $scope.files = ele.files; var reader = new FileReader...

如何使用ajax将字符串数组传递给控制器?(How to pass string array to controller using ajax?)

Stringify to JSON ,添加dataType: 'json' ,然后传递并更正你的"" var postData = JSON.stringify({ deleteArr: deleteArr }); if(deleteArr.length > 0) { $.ajax({ url: @Url.Action("Delete", "ASZ01"), type: "POST", data: p...

相关文章

更多

java反射详解(一)_Class类

Java类用于描述一类事物的共性,该类事物有什么属性,没有什么属性,至于这个属性的值是什么,则是由这个 ...

Guava学习笔记:Immutable(不可变)集合

  不可变集合,顾名思义就是说集合是不可被修改的。集合的数据项是在创建的时候提供,并且在整个生命周期中 ...

如何卸载assembly?或者class

.net的动态编译功能很好,但是有个问题,动态编译的代码,每次执行后,都会产生一个新的assembly ...

mybatis There is no getter for property named 'xx' in 'class java.lang.String

用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no get ...

Java泛型父类取得子类的泛型参数T的Class类型

import java.lang.reflect.ParameterizedType;import j ...

ssh 框架下 class 类打包发布

ssh 框架下 class 类打包后放到 web-inf/lib 下后 @autowire自动注入似乎 ...

【原】storm源码之一个class解决nimbus单点问题

一、storm nimbus 单节点问题概述 1、storm集群在生产环境部署之后,通常会是如下的结构 ...

Java 抽象类

Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有 ...

Java String类

Java String类 字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提 ...

Java对象和类

Java对象和类 Java作为一种面向对象语言。支持以下基本概念: 多态 继承 封 ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本