简体   繁体   English

减少代码:“ VoteService类型中的方法changeVote不适用于参数(无效)”

[英]Cutting down code: “The method changeVote in the type VoteService is not applicable for the arguments (void)”

I am kind of new to Java and am trying to cut down unneccessary lines of code. 我是Java的新手,正在尝试减少不必要的代码行。 I am getting an error, and belive I was able to do this in c# or c++: I wish to bind the following 2 lines of code in one: 我遇到一个错误,但是我能够用c#或c ++做到这一点:我希望将以下两行代码绑定为一个:

checkVote.setType(null);                
changeVote(checkVote);
//into
changeVote(checkVote.setType(null));

I get the error from the title. 我从标题中得到了错误。 I know why it occurs (the setter return type is void, and there is no changeVote(void) method). 我知道为什么会发生(setter返回类型为void,并且没有changeVote(void)方法)。 But I still feel like there are ways of doing what I want. 但是我仍然觉得有很多方法可以做我想做的事情。

You can change the method setType to return this instead of void . 您可以更改setType方法以返回this方法,而不是void This would violate the JavaBean convention, but it could be suitable for your use case. 这将违反JavaBean约定,但可能适合您的用例。

This approach is used in the builder pattern 构建器模式中使用了这种方法

method changeVote possibly takes only checkVote as parameter and you are trying to pass a paramter which is returnig void. 方法changeVote可能仅将checkVote作为参数,而您试图传递的参数为returnig void。 In fact this is not even possible in C++. 实际上,这在C ++中甚至是不可能的。

So those 2 lines continue to be same. 因此,这两行仍然相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JspWriter类型的方法print(boolean)不适用于参数(void) - The method print(boolean) in the type JspWriter is not applicable for the arguments (void) 类型PrintStream的方法printf(String,Object ...)不适用于参数(String,void) - The method printf(String, Object…) in the type PrintStream is not applicable for the arguments (String, void) PrintStream 类型中的方法 println(boolean) 不适用于参数 (void) - The method println(boolean) in the type PrintStream is not applicable for the arguments (void) 声明类型中的方法assertEquals(Object,object)不适用于参数(String,Void) - Method assertEquals(Object, object) in the type Assert is not applicable for the arguments (String, Void) 类型中的方法不适用于参数 - Method in the type is not applicable to the arguments 类型中的方法不适用于参数 - The method in the type is not applicable for the arguments 类型中的方法不适用于参数 - the method in type not applicable for the arguments 类型中的方法不适用于参数 - The method in the type is not applicable for the arguments 泛型-类型中的方法不适用于参数 - Generics - The method in the type is not applicable for the arguments 类型中的方法不适用于参数 java - the method in the type is not applicable for the arguments java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM