在android编程中使用实用程序类 [英] Using utility classes in the android programming
问题描述
如果我在应用程序中使用了一个Utility类,而不是在我的应用中使用该类主活动我必须创建该类的对象或者我可以直接在我的主活动中导入该类吗?
我很抱歉,如果我没有明确的意义。
简而言之,我想清楚的是,基本上如何在我的主要活动中使用实用程序类。
感谢,
david
这在很大程度上取决于你正在使用什么样的实用程序至。有
1)实现静态方法的实用程序类。在这种情况下,您只需直接使用类名调用它们。
2)非静态的实用程序类方法 - 需要创建并可能初始化该类的实例。然后使用实例调用这些方法。
3)可以通过 Context
访问的实用程序类。那么您可以调用 getApplicationContext(),然后您可以 get
访问实用工具类
I have a little idea of the Utility Classes with a slight doubt on demand.
If I use a Utility class in my Application than to use that class in my main Activity do I have to create the object of that class or I can directly Import that class in my main activity?
I am Sorry if I am not making a clear sense.
In the nutshell, all I want to be clear about is that basically how can I use the utility class in my Main Activity?
Thanks, david
It heavily depends on what kind of utility you're referring to. There are
1) utility classes that implement static methods. In that case you just call them directly using class name
2) utility classes methods that are not static - requires creating and possibly initializing an instance of that class. Then the instance is used to call those methods.
3) utility classes that can be accessed thru Context
. then you can call getApplicationContext() and then you can get
access to the utility classes
这篇关于在android编程中使用实用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!