Avoid repeated imports in Java: Inherit imports?

前端 未结 3 893
轮回少年
轮回少年 2021-02-19 07:12

Is there a way to \"inherit\" imports?

Example:

Common enum:

public enum Constant{ ONE, TWO, THREE }

Base clas

相关标签:
3条回答
  • 2021-02-19 07:39

    If you're using Eclipse, use "Organize Imports" (Ctrl+Shift+O) to let the IDE do the imports for you (or use code completion (Ctrl+Space)

    0 讨论(0)
  • 2021-02-19 07:42

    imports are just an aid to the compiler to find classes. They are active for a single source file and have no relation whatsoever to Java's OOP mechanisms.

    So, no, you cannot “inherit” imports

    0 讨论(0)
  • 2021-02-19 07:43

    No, you can't inherit an import. If you want to reference a type within a class file without using the fully-qualified name, you have to import it explicitly.

    But in your example it would be easy enough to say

    public Sub extends Base {
        public Sub() {
            register(Constant.TWO, "blabla"); // without import: Constant.TWO
        }
    }
    
    0 讨论(0)
提交回复
热议问题