2016-08-20 2 views
-2

이것은 내 Java 코드입니다. 두 개의 클래스가있는 하나의 인터페이스 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{ 

} 
관련 문제