简体   繁体   中英

Is it possible to have 2 classes with the same name and in the same package?

I'm developing a grails app, and I need to modify a groovy class that is in a plugin, so I decided to override the class, so I have these method and class in my plugin:

def example = new a();
a.method();

class a {
   void method() {
      println "2";
   }
}

all this was Inside the plugin, so I want to create another class in the same package in my project, to change the method, but how can I set my new class to run instead the plugin's? or is it impossible?

class a {
   void method() {
      println "4";
   }
}

Yes you can. It is called class shadowing. But I would advice against it most of the times. You only need to let the jvm load your class before the plugin class.

是的,您只需要确保您的类在插件版本之前位于类路径中即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM