为什么一些常量公众和其他在Android库code私人 [英] Why are some constants public and other private in an Android library code
问题描述
问题
块引用>为什么一些常量是
公共
修改下,而其他一些私人
?是那些在公共
可以从使用库的应用程序叫什么?如果是这样,如何从一个应用程序调用的常数,它是这样的:<?code> CertainLibraryClass.ActivityResult code code_A
code
块引用>公共类CertainLibraryClass {
公共类ActivityResult code {
公共静态最终诠释code_A = 0X02;
公共静态最终诠释code_B = 0X03;
公共静态最终诠释code_C = 0X04;
}
公共类版本code {
私有静态最终诠释VERSION_MAJOR = 1;
私有静态最终诠释VERSION_MINOR1 = 0;
私有静态最终诠释VERSION_MINOR2 = 2;
}
// ....
}
解决方案为什么一些常量是
公共
修改下?答:让所有其他类都可以访问它如
RESULT_OK
,成功
为什么一些常量是
私人
修改下?答:那么,只有这个类可以访问它。
例如。考虑你调用
的getId()
从你的类libarary功能公共类CertainLibraryClass {
私有静态诠释ID = 0;公共静态INT的getId()
{
返回ID + 1;
}在这里,你不是直接访问
ID
字段,而不是你调用的getId()
函数,最终返回ID,这意味着ID
变量是内部使用CertainLibraryClass
类Problem
Why some constants are under the
public
modifier while some otherprivate
? Are those underpublic
can be called from applications that use the library? If so, how to call the constant from an app, is it like this:CertainLibraryClass.ActivityResultCode.CODE_A
?Code
public class CertainLibraryClass { public class ActivityResultCode { public static final int CODE_A = 0X02; public static final int CODE_B = 0X03; public static final int CODE_C = 0X04; } public class VersionCode { private static final int VERSION_MAJOR = 1; private static final int VERSION_MINOR1 = 0; private static final int VERSION_MINOR2 = 2; } // .... }
解决方案Why some constants are under the
public
modifier?Ans: So that all other classes can access it e.g.
RESULT_OK
,SUCCESS
.Why some constants are under the
private
modifier?Ans:So that only that class can access it
e.g. consider you are calling
getId()
libarary function from your classpublic class CertainLibraryClass { private static int ID=0; public static int getId() { return ID+1; }
here you are not accessing
ID
field directly ,instead you are callinggetId()
function which ultimately returns the id, it means thatID
variable is internally used byCertainLibraryClass
class这篇关于为什么一些常量公众和其他在Android库code私人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!