下面是我收到錯誤的代碼(在「new」之後的第二個「Killer」中)。無法實例化類對象的類型(Java)
String[] classes = new String[5];
kills[0] = "Brian Moser";
kills[1] = "James Doakes";
kills[2] = "Lila Tourney";
kills[3] = "Miguel Prado";
Killer morgan = new Killer("Dexter",
"Morgan",
kills,
4.0,
"Arthur Mitchell",
3,
false);
這已經給了我相當多的麻煩,因爲我看不出爲什麼這個聲明應該工作基於我的構造函數殺手。
而這裏的殺手級:
import java.util.*;
public abstract class Killer{
public String firstName;
public String lastName;
private String[] killList;
private double score;
private String nemesis;
private int accidents;
public boolean caught;
public Killer(String firstName,
String lastName,
String[] killList,
double score,
String nemesis,
int accidents,
boolean caught)
{
this.firstName = firstName;
this.lastName = lastName;
for(int i = 0; i < 5; i++)
this.killList[i] = killList[i];
this.score = score;
this.nemesis = nemesis;
this.accidents = accidents;
this.caught = caught;
} //end constructor
我知道這可能有一個簡單的解決方案,但截至目前爲止,我沒有看到它。
「殺手」類是抽象類。這是你無法實例化的原因。查看[this reference](http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html)以獲取關於Java中抽象類的更多信息。 – Henrik 2013-03-01 12:12:01
謝謝,你是對的。 – 2013-03-01 12:18:11
更改爲'新的殺手( 「嗜血判官」, 「摩根」, 殺敵, 4.0, 「亞瑟·米切爾」, 3, 假){}',它會突然工作。 – 2013-03-01 12:18:20