Java类和接口组合 [英] Java classes and interface combination

查看:90
本文介绍了Java类和接口组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建几个 Java 类来执行某些工作.假设我想通过像这样调用我的类来完成任务:

I'm trying to create couple of Java class to perform certain work. Let's say I want to get the task done by calling my classes like this:

FirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");

                        FirebaseUser user = task.getResult().getUser();
                        // ...
                    } else {
                        // Sign in failed, display a message and update the UI
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // The verification code entered was invalid
                        }
                    }
                }
            });

我可以理解到 signInWithCredential().我不知道如何实现 addOnCompleteListener() 并有一个接口作为参数.

I could understand up to signInWithCredential(). I can't figure out how to implement addOnCompleteListener() and have a interface as argument.

我目前使用 getInstance () 和 signInWithCredential() 等方法创建了像 FirebaseAuth 这样的顶级类.此外,我尝试创建一个接口,但我收到错误,该接口的结果从未被使用过.如何实现addOnCompleteListener(参数1,接口2)的样式.

I've currently create my top class like FirebaseAuth with methods like getInstance () and signInWithCredential(). Also, I tried creating an interface but I am getting error that result of the interface is never used. How can I implement the style of addOnCompleteListener(parameter 1, interface 2).

在这里,addOnCompleteListener 正在获取活动和界面的参数,在我的例子中,我将使用活动参数进行一些工作.

Here, addOnCompleteListener is getting parameters of activity and interface and in my case, I will be using the activity parameter for some work.

P.S:我发现这叫做接口回调.如果是对的,对其结构的任何指导都会很棒

P.S: I found out this is called interface callback. If it's right, any guidance to it's structure will be great

推荐答案

你可以这样做:

创建接口:

public interface onCompleteListener {
    void onComplete(ThrwAtTask object);
}

定义你的ThrwAtTask:

Define your ThrwAtTask:

public abstract class ThrwAtTask {

    public abstract boolean isSuccessful();

    public abstract String getUser();

    public abstract String getOtherData();

}

在您的主要课程中:

public class ThrwAt{

    public static ThrwAt instance;
    private static Activity mActivity;
    public com.basusingh.throwat.onCompleteListener onCompleteListener;


    private ThrwAt(Activity activity) {
        mActivity = activity;
    }

    public static synchronized ThrwAt getInstance(Activity activity) {
        if (instance == null) {
            instance = new ThrwAt(activity);
        }
        return instance;
    }

    public void addOnCompleteListener(@NonNull onCompleteListener var2) {
        onCompleteListener = var2;
        //Call your task function
        doTask();
    }

    public void doTask(){
        ThrwAtTask o = new ThrwAtTask() {
            @Override
            public boolean isSuccessful() {
                return true;
            }

            @Override
            public String getUserId() {
                return "userId1";
            }

            @Override
            public String getApiKey() {
                return "apiKey";
            }
        };
        //Once done, pass your Task object to the interface.
        onCompleteListener.onComplete(o);
    }

}

用法:

ThrwAt.getInstance(MainActivity.this).addOnCompleteListener(new onCompleteListener() {
            @Override
            public void onComplete(ThrwAtTask object) {
                doYourWork(object);
            }
        });

这篇关于Java类和接口组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆