2013-06-30 96 views
-1

我想調用一個超類的對象的子類方法,但因爲我在子類中聲明瞭另一個整數它給我一個例外,是否有一個解決方法,使這種情況發生?如何調用父類的子類方法對象

public static void main(String[]args){ 

    A a = new A(2,3); 
    B b = new B(3,5,6); 

    System.out.println("A object: "); 
    a.showij(); 
    System.out.print("sum = "); 
    ((B) a).sum(); < ==== this line gives me the error, can't cast 
    System.out.println("B object: "); 
    b.showij(); 
    System.out.print("sum = "); 
    b.sum(); 

} 

public class B extends A { 

int k; 

public B(){ 
    super(); 
} 

public B(int a, int b, int c) { 
    super(a, b); 
    k = c; 
} 

public void sum(){ 
    System.out.println(i + j + k); 
} 
} 


public class A { 

int i,j; 

public A() { 

} 

public A(int a, int b){ 
    i = a; 
    j = b;  
} 

public void showij(){ 
    System.out.println("\ti: " + i + " j: " + j); 
} 
} 

*編輯:這裏是整個事情

+0

你能證明什麼'A'和'B'? – sanbhat

+0

你不清楚你在問什麼,也沒有看到「A」和「B」的定義,我認爲任何人都不能理解哪些類可以使用哪些方法。 –

+0

A是需要2個整數的父類java,B是擴展A並帶3個整數的子類,B類是方法sum,它總結了整數i,j和k –

回答

1

如果B擴展A,這仍意味着A是一個單獨的類,當你實例只有一個,你可以不投它因此給B,因爲它與B.無關。

您可以將B轉換爲A,因爲派生類總是可以轉換爲超類。事實上,你甚至不需要爲此而演員。但這是不可能的,反之亦然。

假設B擴展A.

B b = new B(1,2,3,4,5); 
    A a = b; <- This is valid. 

    a.sum(); 

這將通過syntatically正確的,但它仍然會調用B的總和功能,因爲它是B.

的對象

然而,在Java中你不能在類之外顯式調用超級函數,就像在C++中一樣。你必須在你的函數deicde這種然後再在B這樣稱呼它:

class B extends A 
{ 
    @Override 
    public int sum() 
    { 
     super.sum(); 
    } 
} 

如果這是不可取的,必須聲明不同的功能名稱,它不是由派生類覆蓋,但你可以不要依賴特定的行爲,除非你讓課程最終確定,以確保它不能派生。

更新

示例代碼:

public class A 
{ 
    private int mI; 
    private int mJ; 

    public A(int i, int j) 
    { 
     mI = i; 
     mJ = j; 
    } 

    public int sum() 
    { 
     return mI+mJ; 
    } 

    public void showij() 
    { 
     System.out.println("I: "+ mI + " J: "+mJ); 
    } 

    public void print() 
    { 
     System.out.println("A called "+ sum()); 
    } 
} 

B類:

public class B 
    extends A 
{ 
    private int mK; 

    public B(int i, int j, int k) 
    { 
     super(i, j); 
     mK = k; 
    } 

    public int sum() 
    { 
     return super.sum()+mK; 
    } 

    public void showk() 
    { 
     System.out.println("K: "+ mK); 
    } 

    public void print() 
    { 
     System.out.println("B called "+ sum()); 
    } 
} 

Test主:

public class test 
{ 
    public static void main(String[] args) 
    { 
     A a = new A(1, 2); 
     B b = new B(3, 4, 5); 

     a.print(); 
     b.print(); 
     a.showij(); 
     b.showij(); 
     b.showk(); 
     a = b; 
     b.print(); 
    } 
} 
+0

所以我會在父類A中聲明總和,然後像你寫的那樣調用它? –

+0

如果我調用super.sum() –

+0

,我將如何在類B中添加變量,您可以在計算總和的基類中定義一個接口。我發佈了一個如何做到這一點的例子。 – Devolus

0

您的代碼不會COM因爲它有效地將類型投射到它的子類。但是由於這個原因導致了Class類型的運行時異常。

A a = new A(2,3); --> Your instance is of type A 

((B) a).sum(); --> and you are casting it to B, which cannot happen because the object is not of type B 

然而這種說法會工作

A a = new B(2,3, 6); --> As per the declaration type of instance is A but the actual instance created is B. So even if a is cast to B it works. 
((B) a).sum(); 
+0

我會這樣做,謝謝! –