简体   繁体   English

如何分辨何时调用接口中的方法

[英]How to tell when a method in an interface is called

I'm looking to tell when one specific method in an interface is called, given a simple interface: 我想告诉给定一个简单的接口,何时调用接口中的一个特定方法:

public interface Foo {
  String bar();
  int foobar();
}

I want to be able to do something when bar() is called in another class, but I want to let other classes implement Foo and @Override its methods. 我希望能够在另一个类中调用bar()时执行某些操作,但是我想让其他类实现Foo@Override其方法。

Remember that I can't edit the class Foo . 记住,我不能编辑Foo类。

If you want just call some code before or after calling bar implementation, i think it is possible by using AOP (aspect oriented programming) and AspectJ. 如果您只想在调用bar实现之前或之后调用一些代码,我认为可以通过使用AOP(面向方面​​的编程)和AspectJ来实现。 You can use this topic : http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html 您可以使用以下主题: http : //www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html

您不能,接口确实是盲目的,它们无法检查谁在实现其方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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