如何在Android库项目中覆盖类? [英] How to override a class within an Android library project?

查看:120
本文介绍了如何在Android库项目中覆盖类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的情况:

我有一个库项目,并在此基础上的一个项目。现在,在图书馆我有两个类A和B,其中A使用B.在项目中使用的库,我还有一个B类,这应该从库覆盖B级。

I have a library project and a project based on it. Now in the library I have two classes A and B, whereby A uses B. In the project using the library, I have another class B, which should override the class B from the library.

但每次A类进行调用,它最终从库中的B类。 我怎样才能知道机器人应使用B类,而不是从我的项目类B从图书馆?

But every time class A makes a call, it ends up in the class B from the library. How can I tell Android that class B from my project should be used INSTEAD of class B from the library?

推荐答案

这不符合当前的布局工作。你必须使用的策略模式。在你的库定义力霸有一个构造函数类型LibB的对象,在构造函数中:

That does not work with the current layout. You have to use the strategy pattern. In your library define LibA with a constructor that takes a object of type LibB in the constructor:

class LibA{
    LibB b;
    public LibA(LibB b)
        this.b = b;
    }
}

然后你就可以在你的项目覆盖LibB和扩展LibB类中创建力霸:

Then you can override LibB in your project and create LibA with the class that extends LibB:

class ProjectB extends LibB{

}

LibA a = new LibA(new ProjectB());


答到涡轮增压器的问题:


Answer to Turbos question:

您要开始项目,活动从库。所以后来的移动code,创造了意图为您的项目,因为只有在你的项目,你知道的类型或名称的活动启动。

You want to start Project-Activities from your Library. So then move the code that creates the Intent into your Projects, because only in your project you know the type or name of the Activity to be started.

您在您的评论中提到的解决方案(<一href="http://stackoverflow.com/questions/9951610/best-practice-extending-or-overriding-an-android-library-project-class">here)创建意图在库项目中,通过猜测活动应当启动的名字。真的是什么不妥之处,但它不是一个完美的解决方案,你只能启动活动遵循特殊的命名方案。正因为如此,你不能启动任意的活动,在你的项目中可见像活动从其他图书馆,在那里你不能改变类的名称。

The solution you mentioned in your comment (here) creates the Intent in the library project, by guessing the name of the Activity that should be started. There is nothing really wrong with that but it's not an elegant solution. You can only start Activities that follow that special naming scheme. And because of that you cannot start arbitrary Activities that are visible in your projects like Activities from other libraries, where you cannot change the name of the class.

要移动意图创作到您的图书馆,你可以即用的策略,模板或工厂方法模式。查看维基百科 设计模式甚至更多(创世)的模式匹配库设计。

To move the Intent creation into your libraries you can i.e. use the strategy, template or factory-method pattern. See Design Patterns on Wikipedia for even more (creational) patterns that match your library design.

有一个简单的解决方案是:

A simple solution would be:

创建一个抽象的 LibraryActivity ,并从它扩展您的ProjectActivities。该 LibraryActivity 定义返回意图的抽象方法。抽象方法的实现是在做你的 ProjectActivities

Create an abstract LibraryActivity and extend your ProjectActivities from it. The LibraryActivity defines an abstract method that returns the Intent. The implementation of that abstract method is done in your ProjectActivities.

abstract class LibActivity{

    private void doSomething(){
        //Library does something

        //and finally calls createIntent() to start an project specific Activity
        startActivity(this.createIntent());
    }

    protected abstract Intent createIntent();
}


class ProjectActivity extends LibActivity{

    protected Intent createIntent(){
        return new Intent(ProjectActivity.this, AnyActivityYouWant.class);
    }        
}

这篇关于如何在Android库项目中覆盖类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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