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

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

The required type and my provided parameter are different

问题

以下是翻译好的内容:

我正在编写一个侦察系统的代码我有多个类层次结构如下iScoutMember -> Scout实现iScoutMember -> BeaverScout扩展Scout)。我还有一个ScoutList类用于处理将侦察员添加到侦察员ArrayList中以及ScoutSystem类它创建一个菜单并使用ScoutList类中的方法

我遇到的错误来自ScoutSystem类中的addScout方法当我尝试为ArrayList SpecialInterests添加信息时出错

这是错误信息

**错误:(108, 103java不兼容的类型无法将SpecialInterest转换为java.util.ArrayList< SpecialInterest >**

**错误:(112, 103java不兼容的类型无法将SpecialInterest转换为java.util.ArrayList< SpecialInterest >**

这是Scout类的代码

import java.util.ArrayList;
public abstract class Scout implements iScoutMember {
private String name;
private String county;
private String dateOfBirth;
private String address;
private String phoneNumber;
private ArrayList< SpecialInterest > specialInterests;

public Scout(String name, String county, String dateOfBirth, String address, String phoneNumber, ArrayList< SpecialInterest > specialInterests) {
    this.name = name;
    this.county = county;
    this.dateOfBirth = dateOfBirth;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.specialInterests = specialInterests;
}

...

这是SpecialInterest类的代码

public class SpecialInterest {
private String interestCategory;
private String details;
private String dateBadgeReceived;

public SpecialInterest(String interestCategory, String details, String dateBadgeReceived) {
    this.interestCategory = interestCategory;
    this.details = details;
    this.dateBadgeReceived = dateBadgeReceived;
}

...

最后这是Scout系统类中的addScout方法的代码
private void addScout(){

    System.out.println("您想要添加哪种侦察员?");
    System.out.println("1. Beaver侦察员");
    System.out.println("2. Cub侦察员");
    System.out.println("3. Scouter");
    int option = input.nextInt();
    switch(option){
        case 1:
            System.out.println("姓名 => ");
            System.out.println("县城 => ");
            System.out.println("出生日期 => ");
            System.out.println("地址 => ");
            System.out.println("联系电话 => ");
            String name = input.nextLine();
            String county = input.nextLine();
            String dateOfBirth = input.nextLine();
            String address = input.nextLine();
            String phoneNumber = input.nextLine();
            System.out.println("输入特别兴趣详情");
            System.out.println("特别兴趣类别");
            System.out.println("详情");
            System.out.println("获得徽章的日期");
            String interestCategory = input.nextLine();
            String details = input.nextLine();
            String dateBadgeReceived = input.nextLine();

            SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);

            System.out.println("您是否想输入另一个特别兴趣:Y/y表示是,N/n表示否 => ");
            String ans = input.nextLine();
            ans.toUpperCase();
            if (ans.equals("Y")){
                System.out.println("父母电话号码?");
                String parentPhone = input.nextLine();
                BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
                scoutList.addScout(s1);
            } else if(ans.equals("N")){
                String parentPhone = "";
                BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
                scoutList.addScout(s1);
            }
    }
}
}

这是给我错误的代码行

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

import java.util.ArrayList;
public abstract class Scout implements iScoutMember {
private String name;
private String county;
private String dateOfBirth;
private String address;
private String phoneNumber;
private ArrayList&lt;SpecialInterest&gt; specialInterests;
public Scout(String name, String county, String dateOfBirth, String address, String phoneNumber, ArrayList&lt;SpecialInterest&gt; specialInterests) {
this.name = name;
this.county = county;
this.dateOfBirth = dateOfBirth;
this.address = address;
this.phoneNumber = phoneNumber;
this.specialInterests = specialInterests;
}

and this is the code for the SpecialInterest class

public class SpecialInterest {
private String interestCategory;
private String details;
private String dateBadgeReceived;
public SpecialInterest(String interestCategory, String details, String dateBadgeReceived) {
this.interestCategory = interestCategory;
this.details = details;
this.dateBadgeReceived = dateBadgeReceived;
}

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

    System.out.println(&quot;What kind of scout would you like to add?.&quot;);
System.out.println(&quot;1. Beaver Scout&quot;);
System.out.println(&quot;2. Cub Scout&quot;);
System.out.println(&quot;3. Scouter&quot;);
int option = input.nextInt();
switch(option){
case 1:
System.out.println(&quot;Name =&gt; &quot;);
System.out.println(&quot;County=&gt; &quot;);
System.out.println(&quot;Date of Birth =&gt; &quot;);
System.out.println(&quot;Address =&gt; &quot;);
System.out.println(&quot;Contact number =&gt; &quot;);
String name = input.nextLine();
String county = input.nextLine();
String dateOfBirth = input.nextLine();
String address = input.nextLine();
String phoneNumber = input.nextLine();
System.out.println(&quot;Enter Special Interest Details&quot;);
System.out.println(&quot;Special Interest Category&quot;);
System.out.println(&quot;Details&quot;);
System.out.println(&quot;date Badge Received&quot;);
String interestCategory = input.nextLine();
String details = input.nextLine();
String dateBadgeReceived = input.nextLine();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
System.out.println(&quot;Do you wish to enter another special Interest: Y/y for yes, N/n for no ==&gt; &quot;);
String ans = input.nextLine();
ans.toUpperCase();
if (ans.equals(&quot;Y&quot;)){
System.out.println(&quot;Parents Phone Number?&quot;);
String parentPhone = input.nextLine();
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
scoutList.addScout(s1);
} else if(ans.equals(&quot;N&quot;)){
String parentPhone = &quot;&quot;;
BeaverScout s1 = new BeaverScout(name, county, dateOfBirth, address, phoneNumber, sp1, parentPhone);
scoutList.addScout(s1);
}
}
}
}

This is the line of code that gives me the error

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):

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

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

ArrayList&lt;SpecialInterest&gt; sps = new ArrayList&lt;&gt;();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
sps.add(sp1);
//其他操作
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:

SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
//stuff
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

ArrayList&lt;SpecialInterest&gt; sps = new ArrayList&lt;&gt;();
SpecialInterest sp1 = new SpecialInterest(interestCategory,details,dateBadgeReceived);
sps.add(sp1);
//stuff
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:

确定