2016-08-20 16 views
-2

これは私のJavaコードです。私は2つのクラスを持つ1つのインターフェイスabcを作成しました。クラスa1はインタフェースabcを実装します。クラスb1は、インタフェース関数displayを使用してデータを表示します。Javaプログラムによって無限ループが発生する

クラスa1は、無限ループで動作します。

interface abc 
{ 
    display(String s); 
} 

class a1 implments abc 
{ 
    a1(b1 obj) 
    { 
    } 
    public void display(String s) 
    { 
     System.out.println(s); 
    } 
} 

class b1 
{ 
    abc abc1; 
    private xyz x; 
    b1(xyz xyz1) //xyz is interface 
    { 
      this.x = xyz1; 
    } 
    public void show() 
    { 
      abc1 = new a1(new b1(this.x)); // here is problm.. this statement cause infinite loop. 
      String str = "Hello"; 
      abc1.display(str); 
    } 
} 

このプログラムでは、クラスa1の無限ループが発生します。 問題を見つけて解決してください。

+0

デバッガを試しましたか? –

+0

このプログラムにはエラーはありません。無限ループを引き起こします。 –

+0

実際には、エラーがあります。ディスプレイの宣言の開始時に "実装"のスペルミスを忘れて、 "無効"を忘れました – Sweeper

答えて

0

無限ループはありません。 私はメインを作成し、b1.showと呼ばれ、正常に実行されています。

interface abc 
{ 
    void display(String s); 
} 

class a1 implements abc 
{ 
    a1(b1 obj) 
    { 
} 
    public void display(String s) 
    { 
    System.out.println(s); 
    } 
} 

class b1 
{ 
    abc abc1; 
    private xyz x; 
    b1(xyz xyz1) //xyz is interface 
    { 
     this.x = xyz1; 
} 
    public void show() 
    { 
     abc1 = new a1(new b1(this.x)); // here is problm.. this statement cause infinite loop. 
     String str = "Hello"; 
     abc1.display(str); 
    } 
} 
public class Main{ 
    public static void main(String args[]){ 
     xyz xyz1 = null; 
     b1 objb1=new b1(xyz1); 
     objb1.show(); 
     System.out.println("out of all classes.."); 
    } 

} 

interface xyz{ 

} 
関連する問題