简体   繁体   English

通过声明新方法来继承Java抽象类

[英]Java abstract class inheritance by declaring new methods

In my android project I have an abstract class Request 在我的android项目中,我有一个抽象类Request

package Managers;

import java.util.HashMap;

public abstract class Request {
    public interface Completion {
        void execute(HashMap<String, Object> result);
    }

    protected Completion completion;
    protected HashMap<String, Object> requestResult;

    public void setCompletion(Completion newCompletion) {
        completion = newCompletion;
        if (requestResult != null) {
            completion.execute(requestResult);
            requestResult = null;
        }
    }
}

And in my RequestsManager Class I have instantiated it like so 在我的RequestsManager类中,我已将其实例化为

public static Request numberAvailabilityCheck = new Request() {
    public String isSuccess = "isSuccess";
    public String result = "result";

    public void sendRequest(String number) {
        new java.util.Timer().schedule(new java.util.TimerTask() {
            @Override
            public void run() {
                requestResult = new HashMap<String, Object>();
                requestResult.put(isSuccess, true);
                requestResult.put(result, true);
                if (completion != null) {
                    completion.execute(requestResult);
                    requestResult = null;
                }
            }
        }, 5000);
    };
};

In my MainFragment class I need to use numberAvailabilityCheck like so 在我的MainFragment类中,我需要像这样使用numberAvailabilityCheck

private View.OnClickListener checkEventListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        RequestsManager.numberAvailabilityCheck.setCompletion(numberAvailabilityCheckCompletion);
        ViewRelatedUtils.closeKeyboard(getActivity());
        isProcessingCheckNumber = true;
        showProcessing();
        RequestsManager.numberAvailabilityCheck.sendRequest(numberField.getText());
    }
};

But compiler does not see RequestsManager.numberAvailabilityCheck.sendRequest method in MainFragment. 但是编译器看不到MainFragment中的RequestsManager.numberAvailabilityCheck.sendRequest方法。 Why compiler doesn't see it? 为什么编译器看不到? What understands compiler when I declare public method sendRequest in RequestsManager like so? 当我像这样在RequestsManager中声明公共方法sendRequest时,对编译器有什么了解? And how can I make a compiler see it? 以及如何使编译器看到它?

Note that your code can invoke the method setCompletion() without a problem! 请注意,您的代码可以毫无问题地调用setCompletion()方法!

That method is defined on you base class. 该方法在您的基类上定义。 The other method is only defined within the scope of your anonymous inner class instantiation! 另一个方法仅在匿名内部类实例化的范围内定义! Therefore the compiler doesn't "see" it and prevents you from invoking it! 因此,编译器不会“看到”它,并阻止您调用它!

Please note: if you could somehow turn that field into a local variable declared without a type (by using the new var keyword of Java 10), your code would work: as the compiler can infere the fact that you added that method. 请注意:如果您可以通过某种方式将该字段转换为没有类型声明的局部变量(通过使用Java 10的new var关键字),则代码将起作用:编译器可以推断出您添加了该方法。 But by declaring that object to be of type Request, the compiler can only tell you that this method does not exist. 但是通过将对象声明为Request类型,编译器只能告诉您该方法不存在。

But using var is not a good approach, instead the solution could be: add an abstract method for sendRequest() to your class Request! 但是使用var并不是一个好方法,相反的解决方案可能是:将sendRequest()的抽象方法添加到类Request中! If that isn't possible, then consider to not use a single abstract base class, why not have additional abstract classes that extend your base class, each one providing a slightly different send method. 如果不可能,则考虑不要使用单个抽象基类,为什么不使用扩展您的基类的其他抽象类,每个抽象类都提供一个稍有不同的send方法。

Thanks to all for describing me why compiler doesn't see a method. 感谢所有描述我为什么编译器看不到方法的人。 I have found a solution that, I think, is the most comfort solution for me. 我认为我找到了一个最舒适的解决方案。 In RequestsManager I have declared a public class NumberAvailabilityCheck that extends Request and public static member of that class numberAvailabilityCheck. 在RequestsManager中,我声明了一个公共类NumberAvailabilityCheck,该类扩展了Request和该类numberAvailabilityCheck的公共静态成员。

public static class NumberAvailabilityCheck extends Request {
    public String isSuccess = "isSuccess";
    public String result = "result";

    public void sendRequest(String number) {
        new java.util.Timer().schedule(new java.util.TimerTask() {
            @Override
            public void run() {
                requestResult = new HashMap<String, Object>();
                requestResult.put(isSuccess, true);
                requestResult.put(result, true);
                if (completion != null) {
                    completion.execute(requestResult);
                    requestResult = null;
                }
            }
        }, 5000);
    };
};
public static NumberAvailabilityCheck numberAvailabilityCheck = new NumberAvailabilityCheck();

Note that NumberAvailabilityCheck cannot be declared as private. 请注意,不能将NumberAvailabilityCheck声明为私有。

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

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