在Java中列表导入的正确样式是什么? [英] What is the proper style for listing imports in Java?

查看:180
本文介绍了在Java中列表导入的正确样式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好是列出你需要的每一个包(见#1),或者最好从包中导入一切(见#2)?



1



  import java.awt.image.ColorModel; 
import java.awt.image.ComponentColorModel;
import java.awt.image.ColorConvertOp;



2



  import java.awt。*; 


解决方案

> 简单的风格问题;



基本概念
(请参阅 http://javadude.com/articles/importondemandisevil.html 了解详情):

  import java.awt。*; 
import java.util。*;
...
列表列表;



根据需要导入 EVIL ,因为您的程序可在新类加入现有套件时停止编译!



始终 使用显式导入。这不是风格的问题;这是一个安全问题。



这是特别糟糕,如果你检查完美编译代码,一年后,有人检查出来,并试图用新的类库



(按需导入是一个非常糟糕的编程语言功能的示例 - 当新类型添加到系统时,没有功能应该破坏代码) p>

Is it better to list each individual piece of a package you're going to need (see #1) or is it better to just import everything from a package (see #2)?

1

import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.ColorConvertOp;

2

import java.awt.*;

解决方案

It's NOT simply a matter of style; import-on-demand can result in code that ceases to compile as new classes are added to existing packages.

Basic idea (See http://javadude.com/articles/importondemandisevil.html for details.):

import java.awt.*;
import java.util.*;
...
List list;

worked in Java 1.1; as of Java 1.2, the above code will no longer compile.

Import on demand is EVIL because your program can stop compiling when new classes are added to existing packages!

ALWAYS use explicit imports. It's not a matter of style; it's a matter of safety.

This is especially bad if you check in perfectly-compiling code and a year later someone checks it out and tries to compile it with new class libraries.

(Import-on-demand is an example of a really bad programming language feature - no feature should break code when new types are added to a system!)

这篇关于在Java中列表导入的正确样式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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