2011-05-31 38 views
3

下面是繼承Java的繼承示例

的例子
class Parent { 
    Parent(int a, int b) { 
     int c = a + b; 
     System.out.println("Sum=" + c); 
    } 
    void display() { 
     System.out.println("Return Statement"); 
    } 
} 
class Child extends Parent { 
    Child(int a, int b) { 
     int c = a - b; 
     System.out.println("Difference=" + c); 
    } 
} 
public class InheritanceExample { 
    public static void main(String args[]) { 
     Child c = new Child(2, 1); 
     c.display(); 
    } 
} 

時,我沒有非參數化的構造函數的父()

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor 

    at Child.<init>(InheritanceExample.java:14) 
    at InheritanceExample.main(InheritanceExample.java:22) 

能否請您給我解釋一下,我得到下面的錯誤在基類中沒有參數的構造函數的目的是什麼。

+1

它只是創建父類的一個實例,在這種情況下,默認的構造函數。通常你會將對象變量設置爲某個默認值,或者爲null。 – 2011-05-31 01:35:47

+0

請包括錯誤的_text_,而不是屏幕截圖。 – SLaks 2011-05-31 01:37:47

+0

是真正的源代碼嗎?它通過複製/粘貼編譯並運行良好。 – TofuBeer 2011-05-31 01:44:24

回答

8
class child extends parent 
{ 
    child(int a,int b) 
    { 
     int c=a-b; 
     System.out.println("Difference="+c); 
    } 
} 

子類構造函數必須做的第一件事是調用父類的構造函數。 如果您不明確執行此操作(例如super(a,b)),則默認調用默認構造函數(super())。

爲了這個工作,你必須有這個默認的構造函數(沒有任何參數)。

如果你沒有聲明任何構造函數,你會得到默認的構造函數。如果聲明至少一個構造函數,則不會自動獲取默認構造函數,但可以再次添加它。

您收到的錯誤消息是抱怨super()的隱含呼叫不起作用,因爲在父類中沒有這樣的構造函數。

兩種方式來解決這個問題:

  1. 在孩子構造函數的第一行添加一個默認的構造
  2. ,調用非默認的父類的構造(super(a,b)

而且,請不要使用全小寫的類名。

0

我想是在一個類似的問題:

Why should the derived class constructor always access base class constructor?

你可以認爲它是這樣的:因爲「孩子」從「父」繼承,「子」也必須是一個有效的實例「父」(多態),它可以表現爲「父」本身。因此,「孩子」必須做的第一件事就是將自己構建爲一個有效的「父」 - 所以通過super()調用「父」的構造函數必須是構造函數中的第一個方法調用。如果不存在這樣的調用,則產生對「父」的無參數構造器的隱式調用。

3

它要求的父()的原因是因爲孩子延伸父,但你沒有顯式調用超級(A,B)在子構造函數。由於沒有顯式調用父構造函數,因此javac會嘗試調用默認構造方法parent(),並在找不到它時發出抱怨。

你可以用這個代碼中看到這一點:

class parent 
{ 
    public parent() { 
     System.out.println("Parent Constructor"); 
    } 

    public parent(int a,int b) { 
     int c=a+b; 
     System.out.println("Sum="+c); 
    } 

    public void display() { 
     System.out.println("Return Statement"); 
    } 
} 

class child extends parent 
{ 
    public child(int a,int b) { 
     int c=a-b; 
     System.out.println("Difference="+c); 
    } 
} 

public class InheritanceExample 
{ 
    public static void main(String args[]) { 
     child c=new child(2,1); 
     c.display(); 
    } 
} 

輸出:

Parent Constructor 
Difference=1 
Return Statement 

而且,這工作得很好,沒有默認構造函數:

class parent 
{ 
    public parent(int a,int b) { 
     int c=a+b; 
     System.out.println("Sum="+c); 
    } 

    public void display() { 
     System.out.println("Return Statement"); 
    } 
} 

class child extends parent 
{ 
    public child(int a,int b) { 
     super(a,b); 
     int c=a-b; 
     System.out.println("Difference="+c); 
    } 
} 

public class InheritanceExample 
{ 
    public static void main(String args[]) { 
     child c=new child(2,1); 
     c.display(); 
    } 
} 

輸出:

Sum=3 
Difference=1 
Return Statement 
0

錯誤是因爲如果我們沒有明確調用超級的話,那麼JVM會在子類的構造函數中放入super(),並且這個super()會在沒有參數的父類中搜索構造函數所以這是錯誤的。將一個非參數化的構造函數放在父類中,或者將語句super(a,b)放在子構造函數的第一行。

class Parent 
    { 
     Parent(int a, int b) 
     { 
      int c = a + b; 
      System.out.println("Sum=" + c); 
      } 
     void display() 
    { 
     System.out.println("Return Statement"); 
     } 
    } 
    class Child extends Parent 
    { 
     Child(int a, int b) 
    { 
     super(a,b); 
     int c = a - b; 
      System.out.println("Difference=" + c); 
     } 
    } 
    class InheritanceExample 
    { 
     public static void main(String args[]) 
    { 
      Child c = new Child(2, 1); 
     c.display(); 
     } 
    } 
0
public class Mobile{ 
private String manufacturer; 
private String operating_system; 
public String model; 
private int cost; 

Mobile(String man, String o,String m, int c){ 
this.manufacturer=man; 
this.operating_system=o; 
this.model=m; 
this.cost=c; 
} 
public String getModel(){ 
return this.model; 
} 
} 

public class Android extends Mobile{ 
Android(String man, String o,String m, int c){ 
super(man,o,m,c); 
} 
public String getModel(){ 
return "This is Android mobile" +model; 
} 
0
import java.io.*; 
public class XXX 
{ 
public static void main()throws IOException 
{ 
System.out.println("Enter your name."); 
String name = in.readLine(); 
System.out.println(name+" rules!! Thank You!!"); 
} 
}