java 2个namespace,Java命名空间-不同包中具有相同名称的两个类

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

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

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)?

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) I've always heard them referred to as "static methods".

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值