Avoid repeated import in Java: inherit import?

Is there any way to "inherit" imports?

Example:

General enumeration:

public enum Constant{ ONE,TWO,THREE }

Base class using this enumeration:

public class Base {
    protected void register(Constant c,String t) {
      ...
    }
}

Subclasses need to be imported to facilitate the use of enumeration constants (excluding enumeration names):

import static Constant.*; // want to avoid this line!  
public Sub extends Base {
    public Sub() {
        register(TWO,"blabla"); // without import: Constant.TWO
    }
}

Same import as another class

import static Constant.*; // want to avoid this line!
public AnotherSub extends Base {
    ...
}

I can use classic static final constants, but there may be a way to use the same convenient common enumerations

Solution

Imports just help the compiler find classes They are active for a single source file and have nothing to do with Java's OOP mechanism

So, no, you can't "inherit" imports

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>