I am coming from Objective-C where we don't have packages and namespacing.
我来自Objective-C,我们没有包和命名空间。
Android has android.text.format.DateFormat which has static methods that return java.text.DateFormat instances (getLongDateFormat()
and getMediumDateFormat()
specifically).
Android有android.text.format.DateFormat,它有静态方法,返回java.text.DateFormat实例(具体是getLongDateFormat()和getMediumDateFormat())。
-
Are these methods referred to as "static methods" or "class methods" or both interchangeably?
这些方法是否可以互换地称为“静态方法”或“类方法”?
-
Looking at Android documentation, how am I suppose to know that the android.text.format.DateFormat methods return a java.text.DateFormat instance and not an android.text.format.DateFormat instance (returning an instance of the latter is what I initially expected)?
看看Android文档,我怎么想知道android.text.format.DateFormat方法返回一个java.text.DateFormat实例而不是android.text.format.DateFormat实例(返回后者的实例就是我最初的预期)?
-
How do I import the necessary packages to be able to use both of these classes in my source?
如何导入必要的包以便能够在我的源中使用这两个类?
-
Is it possible to write my implementation code this way:
是否可以这样编写我的实现代码:
DateFormat df = DateFormat.getLongDateFormat(this.getActivity());
mLabel.setText(df.format(mEvent.getDate());
The other way I would write it would be to use the full package names, but this seems unnecessary:
我写它的另一种方式是使用完整的包名,但这似乎是不必要的:
java.text.DateFormat df = android.text.format.DateFormat.getLongDateFormat(this.getActivity());
mLabel.setText(df.format(mEvent.getDate());
2 个解决方案
#1
5
Not sure why this is downvoted, it's a useful discussion.
不知道为什么这是低估的,这是一个有用的讨论。
1) I've always heard them referred to as "static methods".
1)我一直听说它们被称为“静态方法”。
2) The only way to see it is to follow the links. The documentation is definitely misleading in this case.
2)看到它的唯一方法是关注链接。在这种情况下,文档肯定会产生误导。
3/4) The typical way to do this in java is to not import one of the classes, and fully-qualify its class name. So if you elected to import java.text.DateFormat
and not the android version, you'd do something like DateFormat df = android.text.format .DateFormat.getLongDateFormat(this.getActivity());
3/4)在java中执行此操作的典型方法是不导入其中一个类,并完全限定其类名。因此,如果你选择导入java.text.DateFormat而不是android版本,你可以执行类似DateFormat df = android.text.format .DateFormat.getLongDateFormat(this.getActivity());
#2
2
-
来自JLS:
A method that is declared
static
is called a class method.声明为static的方法称为类方法。
I would say that I hear "static method" used more often than "class method", but both are in use and should be understood by competent Java developers.
我会说,我听说“静态方法”比“类方法”更常用,但两者都在使用中,应该由有能力的Java开发人员理解。
-
The only option would be to hover the links on the return values. This is an example of extremely poor API design, with a name conflict built in, and the
android.text.format.DateFormat
should have been named something likeDateFormatFactory
. It appears that this class may have been intended to serve the same purpose as thejava.text
class originally and that API compatibility left it stuck. Seejava.sql.Date
for a similar story.唯一的选择是将链接悬停在返回值上。这是一个极差的API设计示例,内置了名称冲突,而android.text.format.DateFormat的名称应该类似于DateFormatFactory。似乎这个类可能原本用于与java.text类相同的目的,并且API兼容性使其停滞不前。有关类似的故事,请参阅java.sql.Date。
-
Using
import
is a convenience only, allowing you to use the simple class name in your code. It's always legal to use a fully-qualified class name, and the compiler translates imported class names into fully-qualified ones. You can't import multiple classes with the same name because then there's no way to distinguish them使用import只是一种便利,允许您在代码中使用简单的类名。使用完全限定的类名始终是合法的,编译器会将导入的类名转换为完全限定的类名。您无法导入具有相同名称的多个类,因为这样就无法区分它们
-
I suggest importing the class from
java.text
for two reasons: You'll probably be using it more often, and it's the more "standard" class. When faced with the choice of qualifying one of two classes with the same simple name, use the simple name for the one that developers would usually assume it refers to.我建议从java.text导入类有两个原因:你可能会更频繁地使用它,而且它是更“标准”的类。当面对具有相同简单名称的两个类中的一个的限定选择时,请使用开发人员通常认为的简单名称。
#1
5
Not sure why this is downvoted, it's a useful discussion.
不知道为什么这是低估的,这是一个有用的讨论。
1) I've always heard them referred to as "static methods".
1)我一直听说它们被称为“静态方法”。
2) The only way to see it is to follow the links. The documentation is definitely misleading in this case.
2)看到它的唯一方法是关注链接。在这种情况下,文档肯定会产生误导。
3/4) The typical way to do this in java is to not import one of the classes, and fully-qualify its class name. So if you elected to import java.text.DateFormat
and not the android version, you'd do something like DateFormat df = android.text.format .DateFormat.getLongDateFormat(this.getActivity());
3/4)在java中执行此操作的典型方法是不导入其中一个类,并完全限定其类名。因此,如果你选择导入java.text.DateFormat而不是android版本,你可以执行类似DateFormat df = android.text.format .DateFormat.getLongDateFormat(this.getActivity());
#2
2
-
来自JLS:
A method that is declared
static
is called a class method.声明为static的方法称为类方法。
I would say that I hear "static method" used more often than "class method", but both are in use and should be understood by competent Java developers.
我会说,我听说“静态方法”比“类方法”更常用,但两者都在使用中,应该由有能力的Java开发人员理解。
-
The only option would be to hover the links on the return values. This is an example of extremely poor API design, with a name conflict built in, and the
android.text.format.DateFormat
should have been named something likeDateFormatFactory
. It appears that this class may have been intended to serve the same purpose as thejava.text
class originally and that API compatibility left it stuck. Seejava.sql.Date
for a similar story.唯一的选择是将链接悬停在返回值上。这是一个极差的API设计示例,内置了名称冲突,而android.text.format.DateFormat的名称应该类似于DateFormatFactory。似乎这个类可能原本用于与java.text类相同的目的,并且API兼容性使其停滞不前。有关类似的故事,请参阅java.sql.Date。
-
Using
import
is a convenience only, allowing you to use the simple class name in your code. It's always legal to use a fully-qualified class name, and the compiler translates imported class names into fully-qualified ones. You can't import multiple classes with the same name because then there's no way to distinguish them使用import只是一种便利,允许您在代码中使用简单的类名。使用完全限定的类名始终是合法的,编译器会将导入的类名转换为完全限定的类名。您无法导入具有相同名称的多个类,因为这样就无法区分它们
-
I suggest importing the class from
java.text
for two reasons: You'll probably be using it more often, and it's the more "standard" class. When faced with the choice of qualifying one of two classes with the same simple name, use the simple name for the one that developers would usually assume it refers to.我建议从java.text导入类有两个原因:你可能会更频繁地使用它,而且它是更“标准”的类。当面对具有相同简单名称的两个类中的一个的限定选择时,请使用开发人员通常认为的简单名称。