2010-01-06 5 views
1
나는 추상 클래스에 대한 매개 변수를 설정하기 위해 노력하고있어

: 여기 자바의 다형성 (polymorphism)/추상 클래스 도움

public abstract class NewMath { 
    public abstract int op (int intOne, int intTwo); 
} 

가 확장 된 서브 클래스 :

public class MultMath extends NewMath { 
    public int op (int intOne, int intTwo){ 
     return intOne + intTwo; 
    } 
} 

하지만 객체 동안 인스턴스화 할 때 다음과 같은 매개 변수를 정의하십시오.

public class TestNewMath { 
    public static void main(String [] _args) { 
     MultMath multObj = new MultMath(3,5); 
    } 
} 

작동하지 않습니다. 그것은 내게이 오류를 준다 :

 
TestNewMath.java:3: cannot find symbol 
symbol : constructor AddMath(int,int) 
location: class AddMath 
     AddMath addObj = new AddMath(3, 5); 

나는 뭔가를 놓친다는 것을 알고있다. 이게 뭐야?

답변

6

두 개의 int 인수로 생성자를 호출했지만 해당 생성자를 생성하지 않았습니다. 두 개의 int 인수를 취하는 'op'라는 메서드 만 만들었습니다.

+0

어디에서 생성자를 넣을 까? – Phil

+0

생성자는 반환 유형 및 클래스 이름이없는 메소드입니다. 이 경우 공용 MultMath (int intOne, int intTwo) – Confusion

1

당신은 지금처럼 "MultMath"클래스의 생성자를 둘 것 :

public MultMath(int arg0, int arg1){ 

} 

은 컴파일 오류를 제거합니다. 또는 다음 작업을 수행 할 수 있습니다.

public class TestNewMath { 
    public static void main(String [] _args) { 
    MultMath multObj = new MultMath(); 
    int x=1, y=2; 
    multObj.op(x,y);   

}