简体   繁体   English

想要在静态方法中调用非静态接口方法。 怎么样?

[英]Want to call a non-static Interface method in a static method. How?

I facing a real hard problem in my code snippet. 我在代码片段中遇到了一个真正的难题。

I want to learn how to use Interface in Java the correct way. 我想学习如何正确使用Java接口。

So for this I have my Application-Class... 所以为此,我有了我的应用程序类...

package inversionUsage;

public class Application {

    public static void main(String [] args) {
        String standard = "Standard version!";

        if (FeatureDecisions.checkEnabledFeatures("new-feature1")) {
            System.out.println("Implement new feature...");
        }else {
            System.out.println(standard);
        }

    }

}

Then I made a Interface... 然后我做了一个界面...

package inversionUsage;

public interface AppConfiguration {

    boolean isEnabled(String searchFeature);

}

I want to use the Interface in another class: 我想在另一个类中使用接口:

package inversionUsage;

import java.util.Arrays;

public class FeatureDecisions implements AppConfiguration{

    public String [] enabledFeatures;
    public String [] _implNewFeature = fetchFeatureTogglesFromSomehere();

    public static boolean checkEnabledFeatures(String searchFeature) {
        return isEnabled(searchFeature);
    }

    @Override
    public boolean isEnabled(String searchFeature) {

        if (Arrays.asList(_implNewFeature).contains(searchFeature)) {
            return true;
        }else {
            return false;
        }

    }

    private String [] fetchFeatureTogglesFromSomehere() {
        // TODO get the CONFIG from somewhere
        enabledFeatures = new String [2];
        enabledFeatures[0] = "new-feature1";
        enabledFeatures[1] = "new-feature2";
        return enabledFeatures;
    }

}

So the workflow is: 1. I start the Application 2. Main method checks the enabled features via FeatureDecisions.java 3. In Feature Decisions i implemented the Interface 因此,工作流程为:1.启动应用程序2. Main方法通过FeatureDecisions.java检查启用的功能。3.在Feature Decisions中,我实现了接口

I getting the error: 我收到错误:

Cannot make a static reference to the non-static method isEnabled(String) from the type FeatureDecisions

May Someone can help me out? 有人可以帮我吗?

The only way to use an instance method is to have an instance on which to call it. 使用实例方法的唯一方法是拥有一个可以在其上调用它的实例。 Your checkEnabledFeatures is static, so it doesn't receive an instance you can use (as this ). 您的checkEnabledFeatures是静态的,因此它不会收到您可以使用的实例( this )。 To use an instance method, it would need to create an instance. 要使用实例方法,需要创建一个实例。 But obviously that's not what you want here. 但这显然不是您想要的。

Java's interface construct is for defining the interface that instances implement. Java的interface构造用于定义实例实现的接口。 Java doesn't have the concept of a "static interface" that a class must implement. Java没有类必须实现的“静态接口”的概念。 On the rare occasions when that's needed, it's usually implemented using reflection (perhaps with a class-level annotation to indicate that the class has the necessary feature). 在极少数需要的情况下,通常使用反射来实现(也许带有类级别的注释,以表明该类具有必要的功能)。

You would have to instantiate the FeatureDecisions class. 您将不得不实例化FeatureDecisions类。

public static boolean checkEnabledFeatures(String searchFeature) {
    return new FeatureDecisions().isEnabled(searchFeature);
}

or make all members static. 或将所有成员设为静态。

Additional info: There are frameworks like togglz that do this for you. 附加信息:有类似togglz的框架可以为您完成此任务。

There's no way to do that. 无法做到这一点。 The closest can get is to use the singleton pattern (though lots of people - myself included - would discourage it). 最接近的方法是使用单例模式(尽管很多人(包括我在内)会阻止这种情况)。

public enum FeatureDecisions implements AppConfiguration
{
    INSTANCE;

    public String [] enabledFeatures;
    public String [] _implNewFeature = fetchFeatureTogglesFromSomehere();

    public boolean checkEnabledFeatures(String searchFeature) {
        return isEnabled(searchFeature);
    }

    @Override
    public boolean isEnabled(String searchFeature) {
        //...
    }
}

Your call would then change from: 然后,您的通话将从:

FeatureDecisions.checkEnabledFeatures(...)

to

FeatureDecisions.INSTANCE.checkEnabledFeatures(...)

It's also worth noting that checkEnabledFeatures doesn't actually do anything besides defer to isEnabled . 还值得注意的是, checkEnabledFeatures除了checkEnabledFeatures isEnabled之外,实际上不做任何其他事情。 You could scrap the former and just call the latter directly. 您可以报废前者,而直接致电后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM