2012-10-06 2 views
0

이 질문에 이전에 물어 본 것이 유감이지만, 무엇을 검색해야할지 모르겠습니다.Java 일반 인수 서브 클래 싱

어쨌든, 나는 수학 패키지를 만들고 있어요, 그리고 클래스의 많은 기능을 확장 :

나는 그들이 동일한있어 있는지 확인하기 위해 기능을 비교하려는
package CustomMath; 

@SuppressWarnings("rawtypes") 
public abstract class Function <T extends Function> { 

    public abstract Function getDerivative(); 

    public abstract String toString(); 

    public abstract Function simplify(); 

    public abstract boolean equals(T comparison); 

} 

. 그들은 같은 클래스에서 온 경우, 나는 특정 비교 메서드를 사용하고 싶지만, 다른 클래스의 경우 false를 반환하고 싶습니다. 다음은 내가 현재 가지고있는 수업 중 하나입니다 :

package CustomMath; 

public class Product extends Function <Product> { 

public Function multiplicand1; 
public Function multiplicand2; 

public Product(Function multiplicand1, Function multiplicand2) 
{ 
    this.multiplicand1 = multiplicand1; 
    this.multiplicand2 = multiplicand2; 
} 

public Function getDerivative() { 
    return new Sum(new Product(multiplicand1, multiplicand2.getDerivative()), new Product(multiplicand2, multiplicand1.getDerivative())); 
} 

public String toString() { 
    if(multiplicand1.equals(new RationalLong(-1, 1))) 
     return String.format("-(%s)", multiplicand2.toString()); 
    return String.format("(%s)*(%s)", multiplicand1.toString(), multiplicand2.toString()); 
} 

public Function simplify() { 
    multiplicand1 = multiplicand1.simplify(); 
    multiplicand2 = multiplicand2.simplify(); 
    if(multiplicand1.equals(new One())) 
     return multiplicand2; 
    if(multiplicand2.equals(new One())) 
     return multiplicand1; 
    if(multiplicand1.equals(new Zero()) || multiplicand2.equals(new Zero())) 
     return new Zero(); 
    if(multiplicand2.equals(new RationalLong(-1, 1))) //if one of the multiplicands is -1, make it first, so that we can print "-" instead of "-1" 
    { 
     if(!multiplicand1.equals(new RationalLong(-1, 1))) // if they're both -1, don't bother switching 
     { 
      Function temp = multiplicand1; 
      multiplicand1 = multiplicand2; 
      multiplicand2 = temp; 
     } 
    } 
    return this; 
} 

public boolean equals(Product comparison) { 
    if((multiplicand1.equals(comparison.multiplicand1) && multiplicand2.equals(comparison.multiplicand2)) || 
      (multiplicand1.equals(comparison.multiplicand2) && multiplicand2.equals(comparison.multiplicand1))) 
     return true; 
    return false; 
} 

} 

어떻게하면됩니까?

+1

는'NPE' – user1406062

+0

이가 그렇다고 http://stackoverflow.com/questions/tagged/crtp의 경우처럼 보이는 것처럼 보일 것입니다 - http://stackoverflow.com/questions/2165613/java-generic-type과 같은 java 관련 질문을보고 싶을 수도 있습니다. http://en.wikipedia.org/wiki/Talk%3ACuriously_recurring_template_pattern –

+0

아마도 그냥'public abstract class Function '을 원한다. – newacct

답변

1

일반 사항에서는 equals 메소드가 'T'유형 (이 경우 'Product')에만 적용된다는 보장이 있습니다. 다른 클래스 유형을 passe 할 수는 없습니다.

또 다른 가능성은 CLASSE 기능에있을 것입니다 정의 :

public abstract boolean equals(Function comparison);

그리고 CLASSE 제품에

comparison instanceof Product

1

오버라이드 Object.equals (Object) 메소드 파크 객체 비교. 여기서 제네릭을 사용할 필요는 없습니다. 그 몸이

if (other instanceof Product) { 
    Product product = (Product) other; 
    // Do your magic here 
} 

return false; 
당신이 점점 방지하기 위해, null로 개체를 비교를 차지한다