类中的构造函数不能应用于给定类型 [英] Constructor in class cannot be applied to given types
问题描述
我使用数组得到以下代码来查找一些原始数字。但是,在尝试编译我的用户类PalindromeArrayUser时,它说 - 类中的构造函数不能应用于给定的类型
I ve got the following code using arrays to find some prim numbers. However, when trying to compile my user class PalindromeArrayUser it says - "Constructor in class cannot be applied to given types"
required:int。找到
:没有参数。
原因:实际和正式参数列表的长度不同。
required: int. found: no arguments. reason: actual and formal arguments lists differ in length.
但是,我已经将构造函数传递给了一个int值(就像它在我的设计中一样)蓝图)。我不太明白问题的来源。谢谢。
However, I have passed to the constructer an int value (the same way it was designed in my blueprint). I don't quite get where the problem comes from. Thanks.
这是我的两个班级
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]);
}
}
}
这是我的用户类,问题来自于此。上面的类编译得很好。
And this is my User class where the problem comes from. The class above compiles fine.
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();
}
}
推荐答案
为类创建构造函数时,不会为该类创建任何默认构造函数。因此,如果你扩展该类,并且如果子类试图调用其超类的no-arg构造函数,则会出现编译时错误。
when you create a constructor for a class, there won't be any default constructor created for that class. so if you extend that class and if the subclass tries to call the no-arg constructor of its super class then there will be an compile-time error.
来演示:
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 []
和检查[]
,因为当时arrLength将是 0
。
to answer your question do this, don't assign the value arrLength
to arr[]
and check[]
as arrLength would be 0
at that time.
所以只需要声明它们
int arr[];
boolean check[];
并在将输入分配给 arrLength $ c后的构造函数中$ c>放这些陈述。
and in the constructor after you assign the input to arrLength
put these statements.
arr = new int[arrLength];
check = new boolean [arrLength];
这篇关于类中的构造函数不能应用于给定类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!