英文:
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<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
                        }
                    }
                }
            });
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 "";
            }
            @Override
            public String someFunc3 {
                return "";
            }
        };
        //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);
            }
        });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论