【问题标题】:Constructor in class cannot be applied to given types类中的构造函数不能应用于给定类型
【发布时间】:2013-11-05 12:25:27
【问题描述】:

我有以下代码使用数组来查找一些原始数字。但是,当尝试编译我的用户类 PalindromeArrayUser 时,它说 - “类中的构造函数不能应用于给定类型”

必需:整数。 发现:没有论据。 原因:实际参数列表和形式参数列表的长度不同。

但是,我已将一个 int 值传递给构造函数(与我在蓝图中的设计方式相同)。我不太明白问题出在哪里。谢谢。

这是我的两门课

 public class PalindromeArray 
 {

int arrLength;

public PalindromeArray(int InputValue) 
{
    arrLength = InputValue;
}


int arr[] = new int[arrLength];
boolean check[] = new boolean [arrLength];


public void InitializeArray()  
{

    for (int k = 2; k < arr.length; k++)
    {
        arr[k] = k;
        check[k] = true;

    }   
}

public void primeCheck()  
{

    for (int i = 2; i < Math.sqrt(arr.length - 1); i++ )
    {
        if (check[i] == true)
        {
        for (int j = 2; j < arr.length; j++)
          {
            if (j % i == 0)
                {
                     check[j] = false;
                     check[i] = true;
                }
          }
        }   

    }   
}

public void PrintArray() 
{
    for (int k = 2; k < arr.length; k++)
    {
        if ((!check[k]) == false)
            System.out.println(arr[k]);

    }
}

   }

这是问题所在的我的用户类。上面的类编译得很好。

 import java.io.*;

 public class PalindromeArrayUser extends PalindromeArray
 {
public static void main(String argv[]) throws IOException 
{
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please enter the upper bound.");

    String line = input.readLine();

    int InputUser = Integer.parseInt(line);
                                     // this is where I pass the same int type as I  
                                                  // constructed it
    PalindromeArray palindrome = new PalindromeArray(InputUser);
    palindrome.InitializeArray();
    palindrome.primeCheck();
    palindrome.PrintArray();


}

 }

【问题讨论】:

    标签: java


    【解决方案1】:

    当您为类创建构造函数时,不会为该类创建任何默认构造函数。因此,如果您扩展该类并且子类尝试调用其超类的无参数构造函数,则会出现编译时错误。

    演示:

    class Parent {
      int i;
      public Parent(int i) {
        this.i=i;
      }
    }
    
    class Child extends Parent {
      int j;
      public Child(int i, int j) {
        super(i);
        this.j=j;
      }
      public Child(int j) {
        // here a call to super() is made, but since there is no no-arg constructor
        // for class Parent there will be a compile time error
        this.j=j;
      }
    }
    

    编辑:

    要回答您的问题,请执行此操作,不要将值 arrLength 分配给 arr[]check[],因为当时 arrLength 将是 0

    所以就这样声明它们

    int arr[];
    boolean check[];
    

    并在将输入分配给arrLength 之后的构造函数中放入这些语句。

    arr = new int[arrLength];
    check = new boolean [arrLength];
    

    【讨论】:

    • 但正如我所说。我删除了有问题的行扩展并且类编译得很好。但是,它不会在提示后执行其余代码。这是为什么呢?
    【解决方案2】:

    错误是因为您扩展了 PalindromeArray 。这不是必需的。 子类(您的 PalindromeArrayUser)必须为构造函数提供一个 int。

    如果你的超类没有默认构造函数,那么你的子类构造函数必须调用超类的非默认构造函数之一。 (super(params))

    【讨论】:

    • 好的。我删除了行扩展 PalindromeArray,然后我的子类编译得很好。但是,当我运行它时,它会显示提示并完成。换句话说,它在提示后不继续,也不执行方法。这是为什么呢?
    • @user2901128 :因为 arr[] 和 check[] 在创建对象时长度为 0,请检查我的解决方案。
    【解决方案3】:

    错误是因为您正在扩展PalindromeArray,它具有显式构造函数。您必须为构造函数提供参数。

    因为 B 中没有可用的默认构造函数,如编译器错误消息所示。一旦在类中定义了构造函数,就不会包含默认构造函数。如果你定义了any构造函数,那么你必须定义所有构造函数。

    阅读更多here

    免责声明:以上内容摘自文章

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多