Java通用 - 对象方法无法找到(Java Generic - object method cannot be found)
我有一个接口,看起来像:
package com.sample.service; import com.sample.model.Request; public interface Service<T> { void add(T domain); <E> boolean update(Request<E> request); }
Request.java
package com.sample.model; public class Request<E> { private final E entity; public Request(E entity) { this.entity = entity; } public E getEntity() { return entity; } }
以下是我的实施
package com.sample.service; import com.sample.domain.User; import com.sample.model.Form; import com.sample.model.Request; public class UserServiceImpl implements Service<User> { @Override public void add(User user) { System.out.println("add: " + user); } @Override public <Form> boolean update(Request<Form> request) { System.out.println("update: " + request); Form form = request.getEntity(); //form.setUsername("some_username");//This line caused compile error return true; } }
这里是Form.java
package com.sample.model; public class Form { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return username; } }
和User.java
package com.sample.domain; public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } }
当我编译并执行这些代码时,一切都很好。 但是,当我取消注释UserServiceImpl中的注释行时,出现编译错误
UserServiceImpl.java:19: error: cannot find symbol form.setUsername("some_username");
我的问题是为什么无法找到表单对象中的setUsername方法? 表单对象是可变的。
I've an interface that looks like:
package com.sample.service; import com.sample.model.Request; public interface Service<T> { void add(T domain); <E> boolean update(Request<E> request); }
Where the Request.java
package com.sample.model; public class Request<E> { private final E entity; public Request(E entity) { this.entity = entity; } public E getEntity() { return entity; } }
The following is my implementation
package com.sample.service; import com.sample.domain.User; import com.sample.model.Form; import com.sample.model.Request; public class UserServiceImpl implements Service<User> { @Override public void add(User user) { System.out.println("add: " + user); } @Override public <Form> boolean update(Request<Form> request) { System.out.println("update: " + request); Form form = request.getEntity(); //form.setUsername("some_username");//This line caused compile error return true; } }
And here are the Form.java
package com.sample.model; public class Form { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String toString() { return username; } }
and User.java
package com.sample.domain; public class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return name; } }
When I compiled and execute these codes everything is fine. However, when I uncomment the line commented in UserServiceImpl, I got compile error
UserServiceImpl.java:19: error: cannot find symbol form.setUsername("some_username");
My question is why the setUsername method in form object cannot be found? The form object is mutable.
原文:https://stackoverflow.com/questions/33084131
最满意答案
您正在寻找
ArgumentOutOfRangeException
- 当参数的值超出被调用方法定义的允许值范围时引发的异常。 (MSDN: http : //msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception%28v=vs.110%29.aspx )You're looking for
ArgumentOutOfRangeException
- The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method. (MSDN: http://msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception%28v=vs.110%29.aspx)
相关问答
更多-
DataTable索引超出范围异常处理,仍然错误抛出(DataTable Index Out of Range Exception Handled, Still Error Thrown)[2022-05-22]
在您的方法执行时,supportPointSelected是固定的,还是可能从另一个线程更改它? 发生异常时,哪个值不正确? 是不正确还是没有第0行? 你能修复列索引并尝试以这种方式进行调试吗? 我知道这不是一个真正的答案,但缺少太多的部分。 Is supportPointSelected fixed while your method is executing or might something be changing it from another thread? When the exception ... -
错误的异常被抛出(Wrong exception being thrown)[2023-10-27]
抛出异常,但是你有一个不执行任何操作的catch块并允许执行继续。 然后,您到达另一个异常并且该程序崩溃了。 尝试在catch块中添加一些日志记录(或e.printStackTrace() ),你会看到会发生什么。 另外,尝试使用IDE的调试器而不是添加打印语句。 您将更好地了解程序的执行情况。 The exception is thrown, but then you have a catch block that doesn't do anything and allows the execution ... -
C#中抛出了'System.OutOfMemoryException'类型的异常(Exception of type 'System.OutOfMemoryException' was thrown in C#)[2022-12-14]
不确切知道你的xslt对象是什么,但猜测它是一个XslCompiledTransform。 你看过这个: MSDN博客文章 Not knowing exactly what your xslt object is but guessing it is a XslCompiledTransform. Have you seen this: MSDN Blog Article -
我想出了我的问题的答案,它提示一个参数异常,因为程序,我通过form.Show()而不是form.ShowDialog()调用表单。 I figured out the answer to my problem, it prompts an argument exception because the program, I called form via form.Show() instead form.ShowDialog().
-
您目前正在尝试迭代并包括数组的大小。 数组在C#中被索引为0。 所以这: int[] numArray = new int[num2]; for (int i = num1; i <= num2; i++) 应该: for (int i = num1; i < num2; i++) 请注意,要获得第一个元素数组, num1必须为0,而不是1。 同样,您的index初始分配为1应该为0 。 基本上你需要经历所有的代码(目前令人困惑的是很多位被注释掉),并检查你假设数组是基于1的所有地方,而是改变你的代码, ...
-
您正在寻找ArgumentOutOfRangeException - 当参数的值超出被调用方法定义的允许值范围时引发的异常。 (MSDN: http : //msdn.microsoft.com/en-us/library/system.argumentoutofrangeexception%28v=vs.110%29.aspx ) You're looking for ArgumentOutOfRangeException - The exception that is thrown when the v ...
-
我仍然不知道为什么会发生异常,但我已经找到了解决方案。 就像在http://www.emgu.com/wiki/index.php/Working_with_Images建议我创建一个垫,填写字节,并创建一个图像从垫巫婆工作正常。 Mat currentMat; using (MemoryStream memoryStream = new MemoryStream(currentEdges)) { byt ...
-
必须承认,我不明白为什么你的代码也不起作用: 我做了一个.net 4的东西跑了,我的确抓住了它: private void button1_Click(object sender, EventArgs e) { // foreach (String file in Directory.GetFiles("c:\\windows", "*.dll")) String file = @"C:\Windows\Microsoft.NET\Framework64\v2. ...
-
string ext = xPhone.Substring(10, preExt) 第二个参数不是结束索引,它是您要提取的字符串的长度。 由于代码中的preExt> 10,xPhone的长度必须超过20个字符(因为您从索引10开始),否则将引发异常。 string ext = xPhone.Substring(10, preExt) The second argument is not the ending index, it is the length of the string you want t ...
-
除了我的评论: 数组/集合始终为0。 例如 t[0] = 1; t[1] = 2; t[2] = 3; t[3] = 4; 如果你像这样循环: for(int i = 0; i <= t.Length; i++) 然后i将计算以下内容: 0 1 2 3 4 因为t.Length = 4而你说i less than equal 4 。 但是由于数组从0开始, i可能不会大于3,否则它会抛出一个IndexOutOfRangeExcepiton 。 如果您将循环更改为 for (int i = 0; i < ...