Programming

이름이 같은 두 개의 클래스를 가져옵니다.

procodes 2020. 8. 14. 22:06
반응형

이름이 같은 두 개의 클래스를 가져옵니다. 처리하는 방법?


다음과 같은 코드가 있다고 가정합니다.

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 ?

  }
}

정규화 된 클래스 이름이어야합니까? 수입 명세서를 제거 할 수 있습니까? 이러한 시나리오가 실제 프로그래밍에서 일반적입니까?


import 문을 생략하고 전체 경로를 사용하여 참조 할 수 있습니다. 예 :

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

그러나 나는 같은 이름과 유사한 기능을 가진 두 개의 클래스를 사용하는 것은 일반적으로 어떤 것이 무엇인지를 명확히 할 수 없다면 일반적으로 최선의 생각이 아니라고 말할 것입니다.


클래스를 가져 오는 대신 정규화 된 이름을 사용하십시오.

예 :

//import java.util.Date; //delete this
//import my.own.Date;

class Test{

   public static void main(String [] args){

      // I want to choose my.own.Date here. How?
      my.own.Date myDate = new my.own.Date();

      // I want to choose util.Date here. How ?
      java.util.Date javaDate = new java.util.Date();
   }
}

예. 동일한 단순 이름을 가진 클래스를 가져올 때 정규화 된 클래스 이름으로 참조해야합니다. 다른 개발자가 작업 할 때 파일에 무엇이 있는지 알 수 있도록 import 문을 그대로 둡니다.

java.util.Data date1 = new java.util.Date();
my.own.Date date2 = new my.own.Date();

이를 수행하는 또 다른 방법은 하위 클래스를 만드는 것입니다.

package my.own;

public class FQNDate extends Date {

}

그런 다음 java.util.Date가있는 패키지에서 my.own.FQNDate를 가져옵니다.


고유 한 날짜 클래스가있는 경우 기본 제공 날짜 클래스와 구별해야합니다. 즉, 왜 자신을 만들었습니까? ImmutableDate 또는 BetterDate 또는 NanoDate와 같은 것, 심지어 MyDate도 자신의 날짜 클래스가있는 이유를 나타냅니다. 이 경우 고유 한 이름을 갖게됩니다.


가져 오기를 사용하여 그중 하나를 가져올 수 있습니다. 다른 모든 유사한 클래스의 경우 정규화 된 클래스 이름을 지정해야합니다. 그렇지 않으면 컴파일 오류가 발생합니다.

예 :

import java.util.Date;

class Test{

  public static void main(String [] args){

    // your own date
    my.own.Date myOwndate ;

    // util.Date
    Date utilDate;
  }
}

이 시나리오는 실제 프로그래밍에서 흔하지는 않지만 그렇게 이상하지는 않습니다. 때로는 서로 다른 패키지의 두 클래스가 같은 이름을 가지며 둘 다 필요합니다.

두 클래스의 이름이 같은 경우 둘 다 동일한 기능을 포함해야하며 둘 중 하나만 선택해야하는 것은 아닙니다.

If we need both, then there is no harm in using that. And it's not a bad programming idea too.

But we should use fully qualified names of the classes (that have same name) in order to make it clear which class we are referring too.

:)


I hit this issue when, for example, mapping one class to another (such as when switching to a new set of classes to represent person data). At that point, you need both classes because that is the whole point of the code--to map one to the other. And you can't rename the classes in either place (again, the job is to map, not to go change what someone else did).

Fully qualified is one way. It appears you can't actually include both import statements, because Java gets worried about which "Person" is meant, for example.


If you really want or need to use the same class name from two different packages, you have two options:

1-pick one to use in the import and use the other's fully qualified class name:

import my.own.Date;

class Test{

     public static void main(String[] args){

        // I want to choose my.own.Date here. How?
        //Answer:
        Date ownDate = new Date();

        // I want to choose util.Date here. How ?
        //Answer:
        java.util.Date utilDate = new java.util.Date();

     }
}


2-always use the fully qualified class name:

//no Date import
class Test{

  public static void main(String[] args){

    // I want to choose my.own.Date here. How?
    //Answer:
     my.own.Date ownDate = new my.own.Date();
    // I want to choose util.Date here. How ?
    //Answer:
     java.util.Date utilDate = new java.util.Date();

  }
}

I just had the same problem, what I did, I arranged the library order in sequence, for example there were java.lang.NullPointerException and javacard.lang.NullPointerException. I made the first one as default library and if you needed to use the other you can explicitly specify the full qualified class name.


When you call classes with the same names, you must explicitly specify the package from which the class is called.

You can to do like this:

import first.Foo;

public class Main {
    public static void main(String[] args) {
        System.out.println(new Foo());
        System.out.println(new second.Foo());
    }
}



package first;

public class Foo {
    public Foo() {
    }

    @Override
    public String toString() {
        return "Foo{first class}";
    }
}



package second;

public class Foo {
    public Foo() {
    }

    @Override
    public String toString() {
        return "Foo{second class}";
    }
}

Output:

Foo{first class}
Foo{second class}

참고URL : https://stackoverflow.com/questions/2079823/importing-two-classes-with-same-name-how-to-handle

반응형