Java类和接口组合

huangapple go评论112阅读模式
英文:

Java classes and interface combination

问题

我正在尝试创建几个Java类来执行特定的工作。假设我想通过以下方式调用我的类来完成任务:

FirebaseAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // 登录成功,使用已登录用户的信息更新界面
                        Log.d(TAG, "signInWithCredential:success");

                        FirebaseUser user = task.getResult().getUser();
                        // ...
                    } else {
                        // 登录失败,显示消息并更新界面
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // 输入的验证码无效
                        }
                    }
                }
            });

我可以理解到signInWithCredential()这一步。我无法弄清楚如何实现addOnCompleteListener()并将接口作为参数传递。

我目前创建了类似FirebaseAuth的顶层类,其中包括getInstance()signInWithCredential()等方法。此外,我尝试创建了一个接口,但是我得到了一个错误,指出接口的结果从未被使用过。我如何实现类似addOnCompleteListener(参数1,接口2)的样式呢?

在这里,addOnCompleteListener接收了一个活动参数和一个接口参数,在我的情况下,我将使用活动参数进行一些工作。

附注:我发现这被称为接口回调。如果我理解正确的话,对于其结构的任何指导都将是很好的。

英文:

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&lt;AuthResult&gt;() {
                @Override
                public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user&#39;s information
                        Log.d(TAG, &quot;signInWithCredential:success&quot;);

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

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

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).

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: I found out this is called interface callback. If it's right, any guidance to it's structure will be great

答案1

得分: 1

你可以这样做:

创建一个接口:

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

定义你的 MyTask 类:

public abstract class MyTask {

    public abstract boolean someFunc1();

    public abstract String someFunc2();

    public abstract String someFunc3();

}

在你的主类中:

public class MainClass {

    public static MainClass instance;
    private static Activity mActivity;
    public onCompleteListener onCompleteListener;

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

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

    public void addOnCompleteListener(@NonNull onCompleteListener var2) {
        onCompleteListener = var2;
        // 调用你的任务函数
        doTask();
    }

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

            @Override
            public String someFunc2() {
                return "";
            }

            @Override
            public String someFunc3() {
                return "";
            }
        };
        // 完成后,将任务对象传递给接口。
        onCompleteListener.onComplete(o);
    }

}

用法:

MainClass.getInstance(MainActivity.this).addOnCompleteListener(new onCompleteListener() {
    @Override
    public void onComplete(MyTask object) {
        doYourWork(object);
    }
});
英文:

You can do it like this:

Create an interface:

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

Define your MyTask class:

public abstract class MyTask {

    public abstract boolean someFunc1();

    public abstract String someFunc2();

    public abstract String someFunc3();

}

In your main class:

public class MainClass{

    public static MainClass instance;
    private static Activity mActivity;
    public onCompleteListener onCompleteListener;


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

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

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

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

            @Override
            public String someFunc2() {
                return &quot;&quot;;
            }

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

}

Usage:

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

huangapple
  • 本文由 发表于 2020年9月4日 19:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63740596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定