2011-04-25 42 views
0

這裏幫助是C++代碼需要用C++到Java翻譯

public class First { 
public: 
    virtual int firstmethod(int a, int b) = 0; 
    virtual int secondmethod(int a, int b, int c) = 0; 
} 

public class Second : public First { 
public: 
    int firstmethod(int a, int b) { 
     int result = a * b; 
     return result + 3; 
    } 
} 

這裏是我的Java到目前爲止

public class First { 
    static in firstmethod(int a, int b) = 0; 
    static int secondmethod(int a, int b, int c) = 0; 
} 


public class Second extends First 
{ 
    static int firstmethod(int a, int b) 
    { 
    int result = a * b; 
    return result + 3; 
    } 
} 

這是正確的? 編輯 我編輯的問題,使其更清晰,更易於遵循

+4

也不是...... – 2011-04-25 01:29:32

+2

我不知道如果C++源是錯誤已經從C++轉換到Java將工作 - 你無縫地從這裏兩種語言混合功能(C++,例如,知道在沒有訪問說明類)。 – 2011-04-25 01:31:10

+0

我只是想爲C++代碼編寫等價的Java代碼,而我對虛擬函數或公衆不熟悉: – dougle 2011-04-25 01:34:55

回答

3

這將是這樣的:

public class Whatever { 
    public int mymethod(int one, int two) { return 0; } 
    public int myothermethod(int one, int two, int three) { return 0; } 
} 


public class However extends Whatever 
{ 
    @Override // optional annotation 
    public int mymethod(int one, int two) 
    { 
    int answer = one * two; 
    return answer + 3; 
    } 
} 

但你可以實例化Whatever。爲了防止任何事情的發生,將其標記爲abstract或將其作爲interface。這一切都取決於你希望你的課程如何繼承Whatever。由於不能有多重繼承,所以要明智地選擇。

public interface Whatever { 
    public int mymethod(int one, int two); 
    public int myothermethod(int one, int two, int three); 
} 

public class However implements Whatever 
{ 
    public int mymethod(int one, int two) 
    { 
    int answer = one * two; 
    return answer + 3; 
    } 
    public int myothermethod(int one, int two, int three) { 
    return 0; 
    } 
} 

public abstract class Whatever { 
    public abstract int mymethod(int one, int two); 
    public abstract int myothermethod(int one, int two, int three); 
} 

public class However extends Whatever 
{ 
    public int mymethod(int one, int two) 
    { 
    int answer = one * two; 
    return answer + 3; 
    } 
    public int myothermethod(int one, int two, int three) { 
    return 0; 
    } 
} 

** 編輯 **

從評論一些啓示後,你的C++與Java等價實際上是第三個概念,因爲你使用virtual類方法在你的C++代碼上。

+4

+1爲接口方法。順便說一下'return 0'版本是錯誤的。在C++中,'virtual ... = 0'轉化爲Java中的'abstract ...'。 – 2011-04-25 01:43:57

+0

我不是C++程序員,雖然我知道C足夠好;我不知道。 – 2011-04-25 02:01:19

3

不,根本沒有。靜態方法永遠不是虛擬的,並且Java對純虛擬方法(它使用「abstract」關鍵字)不使用「= 0」。具有抽象方法的類本身必須標記爲抽象。此外,Java方法默認情況下不公開 - 每個方法都必須單獨標記爲「public」。

+2

你不能有'抽象靜態'方法 – 2011-04-25 01:32:09

+0

C++方法不是靜態的,嘗試翻譯是 – 2011-04-25 02:23:04

1

我會說你的Java翻譯你要找的:

public interface Whatever { 

    public static int myMethod(int one, int two); 
    public static int myOtherMethod(int one, int two, int three); 

} 

public class However implements Whatever { 
    public static int myMethod(int one, int two) { 
      int answer = one * two; 
      return answer + 3; 
    } 
    public static int myOtherMethod(int one, int two, int three) { 
      int answer = one * two; 
      return answer + 3; 
    } 
} 

而且,只是爲了清楚和公約的緣故,我會警惕變量命名「一」或「二」作爲這可能會導致混淆。

+0

這是無效的Java,它不會編譯;你不能有'靜態抽象'方法,無論如何,你的方法沒有被聲明爲這樣,並且不包含正文。 (爲了記錄,我沒有-1你的答案) – 2011-04-25 01:41:47

+0

你是對的,它應該是一個接口。我的b。 – 2011-04-25 01:48:42

+0

閱讀http://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface – 2011-04-25 01:53:06

1

這裏是我的翻譯:

public abstract class Whatever { 
    public abstract int mymethod(int one, int two); 
    public abstract int myothermethod(int one, int two, int three); 
} 

public class However extends Whatever { 
    @Override 
    public int mymethod(int one, int two) { 
     int answer = one * two; 
     return answer + 3; 
    } 

    @Override 
    public int myothermethod(int one, int two, int three) { 
     return ...; 
    } 
} 

我也很喜歡Yanick的有關使用接口的答案;這是一個更好的方法。我保留我的答案,因爲使用了@Override,這對Java代碼很有用。

+0

你仍然缺少'myothermethod'方法;你要麼必須聲明你的''但是'類,要麼暗示某個類中的某個默認主體 – 2011-04-25 01:45:58

+0

@Yanick:我會添加一些......以涵蓋缺失的一個。 ;-) – 2011-04-25 01:47:07