java 从静态方法访问非静态成员变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10060109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing non-static member variables from static methods
提问by tom3322
I'm just starting on Java and I need some help. I know I cannot make a non-static reference on a static method but I need help trying to work around it. I was reading you can access non-static member variables, by creating an instance of the object but I'm not sure exactly how to do it. Here is some on the code. Any help or directions would be reallya appreciated.
我刚刚开始使用 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 );
}
回答by Perception
You can't refer to non-static variables from a static method because that static method is attached to the class, as opposed to any particular instance. From its point of view, those non-static variables don't even exist. However, your question is misleading because nowhere in your code do you show any non-static variable members anyway. It would seem your question is more along the lines of how to instantiate an appropriate classification rule and apply it to the static method argument. There are a number of ways to do this, the simplest would be to simply instantiate an instance of a rule:
您不能从静态方法中引用非静态变量,因为该静态方法附加到类,而不是任何特定实例。从它的角度来看,那些非静态变量甚至不存在。但是,您的问题具有误导性,因为无论如何您都不会在代码中显示任何非静态变量成员。看起来您的问题更像是如何实例化适当的分类规则并将其应用于静态方法参数。有多种方法可以做到这一点,最简单的方法是简单地实例化规则的实例:
ClassificationRule rule = new RuleFirstOccrnc();
rule.apply(userInput);
But given that you have multiple sub-classes of classification rule, you probably need a more sophisticated method of instantiating them. A factory could be useful here, or you can use some more advanced object creation patterns like Injection.
但是鉴于您有多个分类规则的子类,您可能需要一种更复杂的方法来实例化它们。工厂在这里可能很有用,或者您可以使用一些更高级的对象创建模式,如注入。