2013-03-04 82 views
0

我不知道爲什麼我不斷收到此錯誤:無法實例類型父

在我的方法

我:

int option; 
Object message[] = new Object[6];  
message[0] = firstName; 
message[1] = txtfirstName; 
message[2] = lastName; 
message[3] = txtlastName; 
message[4] = gender; 
message[5] = txtgender; 


int response = JOptionPane.showConfirmDialog(null,message,"Register a Person",JOptionPane.OK_CANCEL_OPTION ,               JOptionPane.QUESTION_MESSAGE); 

Parent parent = new Parent(txtfirstName.getText(),txtlastName.getText(),txtgender.getText()); 
      creche.add(parent);` 

在我主我已經安裝:

ArrayList<Parent> parent = new ArrayList<Parent>(); List<Person> school = new ArrayList<Person>();

MyApp app = new MyApp(); 
    ` 

而我有父類和子類和超類Person的子類。

錯誤:Parent parent = new Parent(var1,var2,var3,var4);

但這個工程:Child child = new Child(var1,var2,var3,var4);
creche.add(child);

任何幫助嗎?

+1

父類是抽象的嗎? – 2013-03-04 10:00:46

+0

是的,它現在正在工作! – Shinerrs 2013-03-04 10:07:56

回答

2

爲此,父類需要定義爲具體類。

E.g.

public class Parent extends Person { 

} 

我猜測它被定義爲抽象類或接口。 (見下)

E.g.

public interface Parent { 

} 

public abstract Class Parent { 

} 

您可能只需要將其聲明爲具體類如上所述。

+0

這是它的開始: – Shinerrs 2013-03-04 10:05:29

+0

這是它的開始:public abstract class Parent extends Person { \t List child; \t \t公共父(){ \t \t \t \t this.child =新的ArrayList (); \t} \t \t公共父(串名字,字符串姓氏,性別字符串) \t { – Shinerrs 2013-03-04 10:05:50

+0

刪除父定義抽象的詞(根據我的答案) – cowls 2013-03-04 10:06:13

0

抽象或接口不能實例化。只有子類可以是