Java命名空间-不同包中具有相同名称的两个类 [英] Java Namespace - Two classes with the same name in different packages

查看:493
本文介绍了Java命名空间-不同包中具有相同名称的两个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Objective-C,那里没有软件包和命名空间.

I am coming from Objective-C where we don't have packages and namespacing.

Android具有android.text.format.DateFormat,该类具有返回java.text.DateFormat实例(具体为getLongDateFormat()getMediumDateFormat())的静态方法.

Android has android.text.format.DateFormat which has static methods that return java.text.DateFormat instances (getLongDateFormat() and getMediumDateFormat() specifically).

  1. 这些方法是否被称为静态方法"或类方法",或两者可互换使用?

  1. Are these methods referred to as "static methods" or "class methods" or both interchangeably?

查看 Android文档,我应该怎么知道

如何导入必要的程序包以能够在源代码中同时使用这两个类?

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());

推荐答案

不确定为什么要投票,这是一个有用的讨论.

Not sure why this is downvoted, it's a useful discussion.

1)我一直听说它们被称为静态方法".

1) I've always heard them referred to as "static methods".

2)看到它的唯一方法是点击链接.在这种情况下,文档绝对会产生误导.

2) The only way to see it is to follow the links. The documentation is definitely misleading in this case.

3/4)在Java中执行此操作的典型方法是不导入其中一个类,并完全限定其类名.因此,如果您选择导入java.text.DateFormat而不是android版本,则可以执行DateFormat df = android.text.format .DateFormat.getLongDateFormat(this.getActivity());

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());

这篇关于Java命名空间-不同包中具有相同名称的两个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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