몇 가지 prim 번호를 찾기 위해 배열을 사용하는 다음 코드가 있습니다. 그러나 내 사용자 클래스 PalindromeArrayUser를 컴파일하려고 할 때 "클래스의 생성자는 주어진 유형에 적용 할 수 없습니다."
필수 : int. 발견 : 인수가 없습니다. 이유 : 실제 및 형식 인수 목록의 길이가 다릅니다.
그러나 저는 생성자에게 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();
}
}
클래스에 대한 생성자를 만들 때 해당 클래스에 대해 생성 된 기본 생성자가 없습니다. 따라서 해당 클래스를 확장하고 하위 클래스가 수퍼 클래스의 인수가없는 생성자를 호출하려고하면 컴파일 타임 오류가 발생합니다.
시연 :
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];
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다