导入两个同名的类.如何处理? [英] Importing two classes with same name. How to handle?

查看:40
本文介绍了导入两个同名的类.如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似的代码:

Say I've a code like:

import java.util.Date;
import my.own.Date;

class Test{

  public static void main(String [] args){

    // I want to choose my.own.Date here. How?
    ..
    // I want to choose util.Date here. How ?

  }
}

我应该是完全限定的类名吗?我可以摆脱导入语句吗?这样的场景在现实世界的编程中常见吗?

Should I be full qualified class names? Can I get rid of the import statements? Is such a scenario common in real world programming?

推荐答案

您可以省略导入语句并使用整个路径引用它们.例如:

You can omit the import statements and refer to them using the entire path. Eg:

java.util.Date javaDate = new java.util.Date()
my.own.Date myDate = new my.own.Date();

但我想说的是,使用两个具有相同名称和相似函数的类通常不是最好的主意,除非您能明确说明哪个是哪个.

But I would say that using two classes with the same name and a similiar function is usually not the best idea unless you can make it really clear which is which.

这篇关于导入两个同名的类.如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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