[英]Why am I receiving this Run time exception?
我正在開發一個程序,該程序會將隨機數添加到數組中,直到將重復數放入數組中,然后它應該在生成重復數之前打印出生成了多少個數。
當我運行程序時,我收到此錯誤。
Exception in thread "main" java.lang.ExceptionInInitializerError
at arrayintlog.TestLuck.main(TestLuck.java:25)
Caused by: java.lang.RuntimeException: Uncompilable source code - arrayintlog.ArrayIntLog is not abstract and does not override abstract method contains(java.lang.String) in arrayintlog.IntLogInterface
at arrayintlog.ArrayIntLog.<clinit>(ArrayIntLog.java:6)
... 1 more
Java Result: 1
我不知道這個例外意味着什么。 我以前只做過一個接口,而且幾乎完全一樣,但是有一個字符串數組,對此我沒有任何問題。
有了這個,netbeans不斷告訴我將抽象添加到ArrayIntLog類中,但是如果我這樣做,則ArrayIntLog構造函數在我的主類中不起作用?
我在做什么錯/錯過了什么?
這是我的主班
package arrayintlog;
import java.util.Random;
import java.util.Scanner;
public class TestLuck
{
public static void main(String[] args)
{
int cycles = 0;
String name;
int min = 1;
int max = 10000;
int duplicate;
Random rand = new Random();
int random = rand.nextInt(max - min + 1) + min;
Scanner in = new Scanner(System.in);
System.out.println("What is the name of your Log: ");
name = in.next();
ArrayIntLog myLog = new ArrayIntLog(name);
for (int index = 0; index <= myLog.size(); index++)
{
myLog.insert(12);
int duplicateCheck = log[index];
if (myLog.contains(duplicateCheck))
{
myLog.toString();
System.out.println("It took " + cycles + " cycles to generate duplicate numbers randomly.");
}
else
{
cycles++;
}
}
}
}
這是我的數組int日志類:
package arrayintlog;
public class ArrayIntLog implements IntLogInterface
{
protected String name; //name of the IntLog
protected int[] log; //array that holds the integers
protected int lastIndex = -1;
//==========================Constructor=====================================
public ArrayIntLog(String name, int maxSize)
{
log = new int[maxSize];
this.name = name;
}
//==========================Constructor=====================================
public ArrayIntLog(String name)
{
log = new int[100];
this.name = name;
}
//===========================Insert=========================================
public void insert(int element)
{
lastIndex++;
log[lastIndex] = element;
}
//===========================isFull=========================================
public boolean isFull()
{
if(lastIndex == (log.length - 1))
{
return true;
}
else
{
return false;
}
}
//============================Size==========================================
public int size()
{
return lastIndex + 1;
}
//===========================Contains=======================================
public boolean contains(int element)
{
int location = 0;
while (location <= lastIndex)
{
if (element == (log[location]))
{
return true;
}
else
{
location++;
}
}
return false;
}
//=============================Clear========================================
public void clear()
{
for (int index = 0; index <= lastIndex; index++)
{
log[index] = null;
}
lastIndex = -1;
}
//=============================getName======================================
public String getName()
{
return name;
}
public String toString()
{
String logString = "Log " + name +"/n/n";
for (int index = 0; index <= lastIndex; index++)
{
logString = logString + (index+1) + ". " +
log[index] + "/n";
}
return logString;
}
}
問題在於IntLogInterface
接口(您未發布)至少包含另一個方法( contains(String)
),而該方法尚未在ArrayIntLog
類中實現。
您的IDE會一直告訴您將abstract
關鍵字添加到ArrayIntLog
類中,因為類必須實現由已實現的接口繼承或定義的所有abstract
方法,或者必須將類本身聲明為abstract
並保留對子類的“ missing”方法的實現。
顯然,直到未實現“缺失”方法之前,無法實例化該類,因此如果實例為abstract
則無法實例化ArrayIntLog
。
只需實現IntLogInterface
定義的所有方法。
您應該實現IntLogInterface接口的所有功能
例外是不言自明的。 您必須在ArrayIntLog
類中實現contains(String element)
。 在實現interface
,需要實現該interface
所有方法。
您必須在IntLogInterface
實現所有抽象方法。
它說“ arrayintlog.ArrayIntLog不是抽象的,不會覆蓋抽象方法contains(java.lang.String)
”。 但是您實現的contains
方法具有不同的簽名: public boolean contains(int element)
該錯誤不言自明。
arrayintlog.ArrayIntLog is not abstract and does not override abstract method contains(java.lang.String) in arrayintlog.IntLogInterface
如果您使用的是接口,請不要忘記實現其所有方法。 做到這一點,你就很好了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.