Java 类中的构造函数不能应用于给定类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19788963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Constructor in class cannot be applied to given types
提问by user2901128
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"
我有以下代码使用数组来查找一些素数。但是,当尝试编译我的用户类 PalindromeArrayUser 时,它说 - “类中的构造函数不能应用于给定类型”
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.
但是,我已向构造函数传递了一个 int 值(与在我的蓝图中设计的方式相同)。我不太明白问题从何而来。谢谢。
Here are my two classes
这是我的两节课
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.
这是我的 User 类问题的来源。上面的类编译得很好。
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();
}
}
采纳答案by Thirumalai Parthasarathi
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.
为类创建构造函数时,不会为该类创建任何默认构造函数。因此,如果您扩展该类并且子类尝试调用其超类的无参数构造函数,则会出现编译时错误。
to demonstrate:
展示:
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;
}
}
EDIT:
编辑:
to answer your question do this, don't assign the value arrLength
to arr[]
and check[]
as arrLength would be 0
at that time.
回答你的问题做到这一点,并不值分配arrLength
到arr[]
与check[]
作为arrLength将0
在那个时候。
so just declare them like this
所以就这样声明它们
int arr[];
boolean check[];
and in the constructor after you assign the input to arrLength
put these statements.
并在您分配输入后在构造函数中arrLength
放置这些语句。
arr = new int[arrLength];
check = new boolean [arrLength];
回答by Areo
Error is because you extend PalindromeArray
.This is not necessary.
Subclass (your PalindromeArrayUser
) must provide constructor with one int.
错误是因为您扩展了PalindromeArray
。这不是必需的。子类(您的PalindromeArrayUser
)必须为构造函数提供一个 int。
If yous super class don't have default constructor then in you subclass constructors must invoke one of non-default constructor from super class. (super(params)
)
如果您的超类没有默认构造函数,那么在您的子类构造函数中,必须从超类调用非默认构造函数之一。( super(params)
)
回答by lazyprogrammer
Error is because you are extending PalindromeArray
, which has an explicit constructor. You must provide the constructor with argument.
错误是因为您正在扩展PalindromeArray
,它具有显式构造函数。您必须为构造函数提供参数。
Because there is no default constructor available in B, as the compiler error message indicates. Once you define a constructor in a class, the default constructor is not included. If you define anyconstructor, then you must define allconstructors.
因为 B 中没有可用的默认构造函数,正如编译器错误消息所示。一旦在类中定义了构造函数,就不会包含默认构造函数。如果定义了任何构造函数,则必须定义所有构造函数。
Read more here
在这里阅读更多
Disclaimer: The above is taken from the article
免责声明:以上内容来自文章