所需类型和我提供的参数不同。

huangapple go评论98阅读模式
英文:

The required type and my provided parameter are different

问题

以下是翻译好的内容:

  1. 我正在编写一个侦察系统的代码我有多个类层次结构如下iScoutMember -> Scout实现iScoutMember -> BeaverScout扩展Scout)。我还有一个ScoutList用于处理将侦察员添加到侦察员ArrayList以及ScoutSystem它创建一个菜单并使用ScoutList类中的方法
  2. 我遇到的错误来自ScoutSystem类中的addScout方法当我尝试为ArrayList SpecialInterests添加信息时出错
  3. 这是错误信息
  4. **错误:(108, 103java不兼容的类型无法将SpecialInterest转换为java.util.ArrayList< SpecialInterest >**
  5. **错误:(112, 103java不兼容的类型无法将SpecialInterest转换为java.util.ArrayList< SpecialInterest >**
  6. 这是Scout类的代码
  7. import java.util.ArrayList;
  8. public abstract class Scout implements iScoutMember {
  9. private String name;
  10. private String county;
  11. private String dateOfBirth;
  12. private String address;
  13. private String phoneNumber;
  14. private ArrayList< SpecialInterest > specialInterests;
  15. public Scout(String name, String county, String dateOfBirth, String address, String phoneNumber, ArrayList< SpecialInterest > specialInterests) {
  16. this.name = name;
  17. this.county = county;
  18. this.dateOfBirth = dateOfBirth;
  19. this.address = address;
  20. this.phoneNumber = phoneNumber;
  21. this.specialInterests = specialInterests;
  22. }
  23. ...
  24. 这是SpecialInterest类的代码
  25. public class SpecialInterest {
  26. private String interestCategory;
  27. private String details;
  28. private String dateBadgeReceived;
  29. public SpecialInterest(String interestCategory, String details, String dateBadgeReceived) {
  30. this.interestCategory = interestCategory;
  31. this.details = details;
  32. this.dateBadgeReceived = dateBadgeReceived;
  33. }
  34. ...
  35. 最后这是Scout系统类中的addScout方法的代码
  36. private void addScout(){
  37. System.out.println("您想要添加哪种侦察员?");
  38. System.out.println("1. Beaver侦察员");
  39. System.out.println("2. Cub侦察员");
  40. System.out.println("3. Scouter");
  41. int option = input.nextInt();
  42. switch(option){
  43. case 1:
  44. System.out.println("姓名 => ");
  45. System.out.println("县城 => ");
  46. System.out.println("出生日期 => ");
  47. System.out.println("地址 => ");
  48. System.out.println("联系电话 => ");
  49. String name = input.nextLine();
  50. String county = input.nextLine();
  51. String dateOfBirth = input.nextLine();
  52. String address = input.nextLine();
  53. String phoneNumber = input.nextLine();
  54. System.out.println("输入特别兴趣详情");
  55. System.out.println("特别兴趣类别");
  56. System.out.println("详情");
  57. System.out.println("获得徽章的日期");
  58. String interestCategory = input.nextLine();
  59. String details = input.nextLine();
  60. String dateBadgeReceived = input.nextLine();
  61. SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
  62. System.out.println("您是否想输入另一个特别兴趣:Y/y表示是,N/n表示否 => ");
  63. String ans = input.nextLine();
  64. ans.toUpperCase();
  65. if (ans.equals("Y")){
  66. System.out.println("父母电话号码?");
  67. String parentPhone = input.nextLine();
  68. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
  69. scoutList.addScout(s1);
  70. } else if(ans.equals("N")){
  71. String parentPhone = "";
  72. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
  73. scoutList.addScout(s1);
  74. }
  75. }
  76. }
  77. }
  78. 这是给我错误的代码行
  79. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);

如果您有任何进一步的问题,请随时提问。

英文:

I am currently writing code for a scout system I have multiple classes and the hierarchy goes as follows iScoutMember -> Scout(implements iScoutMember) -> BeaverScout(extends Scout). I also have a ScoutList class that handles adding scouts to the ArrayList of scouts and ScoutSystem which makes a menu and uses the methods from the ScoutList class.

The error I get is from the addScout method in the ScoutSystem class when I try to add info for the ArrayList SpecialInterests

This is the error info

Error:(108, 103) java: incompatible types: SpecialInterest cannot be converted to java.util.ArrayList&lt;SpecialInterest&gt;

Error:(112, 103) java: incompatible types: SpecialInterest cannot be converted to java.util.ArrayList&lt;SpecialInterest&gt;

This is the code for the Scout class

  1. import java.util.ArrayList;
  2. public abstract class Scout implements iScoutMember {
  3. private String name;
  4. private String county;
  5. private String dateOfBirth;
  6. private String address;
  7. private String phoneNumber;
  8. private ArrayList&lt;SpecialInterest&gt; specialInterests;
  9. public Scout(String name, String county, String dateOfBirth, String address, String phoneNumber, ArrayList&lt;SpecialInterest&gt; specialInterests) {
  10. this.name = name;
  11. this.county = county;
  12. this.dateOfBirth = dateOfBirth;
  13. this.address = address;
  14. this.phoneNumber = phoneNumber;
  15. this.specialInterests = specialInterests;
  16. }

and this is the code for the SpecialInterest class

  1. public class SpecialInterest {
  2. private String interestCategory;
  3. private String details;
  4. private String dateBadgeReceived;
  5. public SpecialInterest(String interestCategory, String details, String dateBadgeReceived) {
  6. this.interestCategory = interestCategory;
  7. this.details = details;
  8. this.dateBadgeReceived = dateBadgeReceived;
  9. }

and finally this is the code for the addScout method in the Scout system class
private void addScout(){

  1. System.out.println(&quot;What kind of scout would you like to add?.&quot;);
  2. System.out.println(&quot;1. Beaver Scout&quot;);
  3. System.out.println(&quot;2. Cub Scout&quot;);
  4. System.out.println(&quot;3. Scouter&quot;);
  5. int option = input.nextInt();
  6. switch(option){
  7. case 1:
  8. System.out.println(&quot;Name =&gt; &quot;);
  9. System.out.println(&quot;County=&gt; &quot;);
  10. System.out.println(&quot;Date of Birth =&gt; &quot;);
  11. System.out.println(&quot;Address =&gt; &quot;);
  12. System.out.println(&quot;Contact number =&gt; &quot;);
  13. String name = input.nextLine();
  14. String county = input.nextLine();
  15. String dateOfBirth = input.nextLine();
  16. String address = input.nextLine();
  17. String phoneNumber = input.nextLine();
  18. System.out.println(&quot;Enter Special Interest Details&quot;);
  19. System.out.println(&quot;Special Interest Category&quot;);
  20. System.out.println(&quot;Details&quot;);
  21. System.out.println(&quot;date Badge Received&quot;);
  22. String interestCategory = input.nextLine();
  23. String details = input.nextLine();
  24. String dateBadgeReceived = input.nextLine();
  25. SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
  26. System.out.println(&quot;Do you wish to enter another special Interest: Y/y for yes, N/n for no ==&gt; &quot;);
  27. String ans = input.nextLine();
  28. ans.toUpperCase();
  29. if (ans.equals(&quot;Y&quot;)){
  30. System.out.println(&quot;Parents Phone Number?&quot;);
  31. String parentPhone = input.nextLine();
  32. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
  33. scoutList.addScout(s1);
  34. } else if(ans.equals(&quot;N&quot;)){
  35. String parentPhone = &quot;&quot;;
  36. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
  37. scoutList.addScout(s1);
  38. }
  39. }
  40. }
  41. }

This is the line of code that gives me the error

  1. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);

Any and all help will be much appreciated I am completely new to inheritance and to stack overflow so please be patient with me if my question is not formatted properly.

答案1

得分: 1

你的问题并不是非常清楚,但从我所理解的来看,你在addScout方法中传递了一个单独的SpecialInterest对象(sp1):

  1. SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
  2. //其他操作
  3. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);

你可能应该创建一个新的ArrayList&lt;SpecialInterest&gt;并将其传递进去,就像这样:

  1. ArrayList&lt;SpecialInterest&gt; sps = new ArrayList&lt;&gt;();
  2. SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
  3. sps.add(sp1);
  4. //其他操作
  5. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sps, parentPhone);
英文:

Your question isn't extremely clear, but from what I can tell, you're passing in a single SpecialInterest object (sp1) in the addScout method at:

  1. SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
  2. //stuff
  3. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);

What you probably should do is make a new ArrayList&lt;SpecialInterest&gt; and pass that in, like this

  1. ArrayList&lt;SpecialInterest&gt; sps = new ArrayList&lt;&gt;();
  2. SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
  3. sps.add(sp1);
  4. //stuff
  5. BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sps, parentPhone);

huangapple
  • 本文由 发表于 2020年4月7日 00:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61064592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定