如何将另一个java文件(或类)的名称放在方法的名称旁边?
问题描述:
我的教授列出了一些我应该写或修改的方法。其中一位她已列为Customer getCustomer(String n)。现在,customer是另一个java文件的名称,稍后将用它来使代码正常工作,但我几乎可以确定getCustomer是我应该创建该方法的名称。下面我将复制并粘贴我为这个方法编写的代码,然后我将发布编译错误。请帮我拿这个代码进行编译。如何将另一个java文件(或类)的名称放在方法的名称旁边?
public void Customer getCustomer(String username){
for(int i=0; i<customerList.size(); i++){
String holdingSpotForArrayEle;
holdingSpotForArrayEle = customerList.get(i);
if (holdingSpotForArrayEle == username)
return username;
}
}
您是否在这里看到第一行代码,即错误所在。
编译错误如下:
CustomerDatabase.java:77: error: '(' expected
public void Customer getCustomer(String username){
^
1 error
(顺便说一句,胡萝卜丁标志是GETCUSTOMER在下文G) 如果你想摆脱的客户,教授包括客户在通过将方法引用为Customer getCustomer(String n)来开始该方法。
答
public void Customer getCustomer(String username)
是方法的不正确签名。
也期待在return语句代码中
return username; // check type of username
电流签名将返回String
这也是username
类型:
public String getCustomer(String username)
为了回报类型Customer
,您的方法定义应更新为使用:
return new Customer(); // just for example
,然后匹配方法签名会
public Customer getCustomer(String username)
'公共无效客户GETCUSTOMER(字符串username){'您已经定义了2种返回类型 – Ramanlfc
的教授。给你使用的确切方法签名:'Customer getCustomer(String n){...}'。 –
另外,不要使用==来比较字符串。而不是'holdSpotForArrayEle ==用户名',使用'holdingSpotForArrayEle.equals(username)'。 –