Too many imports spamming my code

后端 未结 8 2623
既然无缘
既然无缘 2021-02-18 13:37

In my project I have a shapes package which has shapes I designed for my graphics program e.g Rectangle, Circle. I also have one or two more packages that have the

8条回答
  •  鱼传尺愫
    2021-02-18 14:12

    I don't get all the non-answers posted here.

    Yes, individual imports are a bad idea, and add little, if anything, of value.

    Instead just explicitly import the conflicts with the class you want to use (The compiler will tell you about conflicts between the awt and shapes package.) like this:

    import java.awt.*;
    import shapes.*;
    import shapes.Rectangle; // new Rectangle, and Rectangle.xxx will use shapes.Rectangle.
    

    This has been done for years, since Java 1.2, with awt and util List classes. If you occasionally want to use java.awt.Rectangle, well, use the full class name, e.g., new java.awt.Rectangle(...);.

提交回复
热议问题