2
我剛剛開始使用Java,我需要一些幫助。我知道我不能對靜態方法做非靜態引用,但我需要幫助解決它。我在讀你可以通過創建對象的實例來訪問非靜態成員變量,但我不確定如何去做。這裏是一些代碼。任何幫助或指示將非常感激。從靜態方法訪問非靜態成員變量
package tweetClassification;
public class PriorityRules {
public static void prioritize(final String userInput){
ClassificationRule.apply(aUserInput); //ERROR
// Cannot make a static reference to
// the non-static method apply(String)
// from the type ClassificationRule
}
}
*----------------------------------------------------------------
package tweetClassification;
public class ClassificationRule {
public void apply (final String aUserInput) {
apply(aUserInput);
}
}
*----------------------------------------------------------------
package tweetClassification;
import java.util.ArrayList;
public class RuleFirstOccrnc extends ClassificationRule {
public void apply (final String aUserInput){
for(TweetCat t: TwtClassif.tCat)
applyFirstOccurrenceRuleTo(t, aUserInput);
}
*----------------------------------------------------------------
package tweetClassification;
public class RuleOccrncCount extends ClassificationRule {
public void apply (final String aUserInput){
for(TweetCat t: TwtClassif.tCat)
applyOccurrenceCountRuleTo(t, aUserInput);
}
@Perception的答案是正確的。此外,我建議你聲明簡單的舊'ClassificationRule'爲'abstract',並使其''apply'方法也是'abstract'。就像現在這樣,ClassificationRule的'apply'方法是一個無限循環。 – 2012-04-08 03:25:07
謝謝你的工作!..這非常簡單..謝謝你的解釋非常有幫助。 – tom3322 2012-04-08 03:26:18