是否可以为 Java 中的自定义(非本机)方法/函数设置默认行为?

发布于 2024-09-01 04:51:48 字数 305 浏览 4 评论 0 原文

是否可以为 Java 中的自定义(非本机)方法/函数设置默认行为?

例如,我想更改默认的“Function”以在调用时执行 System.out.println("message")

因此,当创建自定义方法/函数时:

public String testMethod()
{
   //custom code
}

它应该在运行自定义代码之前执行新添加的默认行为(在本例中为系统输出)。

即使这是一个糟糕的练习,有可能吗?也许通过扩展函数类或其他什么?

Is it possible to set a default behaviour for custom (non-native) methods/functions in Java?

For example, I would like to change the default "Function" to do a System.out.println("message") whenever called.

So, when a custom method/function is being created:

public String testMethod()
{
   //custom code
}

it should execute the newly added default behaviour (in this case the system output), before the custom code is run.

Even if this would be a bad excercise, is it possible? Maybe by extending the function class or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清眉祭 2024-09-08 04:51:48

一种方法是使用 Java 的面向方面的编程 (AOP):Aspect/J。对于您想要的,AOP 允许您在程序中的特定点注入代码,例如,让指定的方法在进入或退出该方法时执行一些 println 代码。 [AOP 的用途比打印调试语句的简单用途要大得多,但我试图保持回答问题的目标。]

这篇文章(可能有点过时)显示了一个与您想要的类似的示例:

http://www.developer.com /java/other/article.php/3109831/Simplify-your-logging-with-AspectJ.htm

One way is using Aspect-oriented programming (AOP) for Java: Aspect/J. For what you want, AOP would let you inject code in your program at specific points, e.g. having specified methods execute some println code upon entering or exiting the method. [AOP has a much larger purpose than this simple use for printing debug statements, but I'm trying to stay on target for answering the question.]

This article (possibly somewhat dated) shows an example similar to what you want:

http://www.developer.com/java/other/article.php/3109831/Simplify-your-logging-with-AspectJ.htm

草莓酥 2024-09-08 04:51:48

听起来您想要面向方面的编程(例如 AspectJ)或字节码编织(例如, ASMcglib)。

It sounds like you want either aspect-oriented programming (e.g., AspectJ) or byte-code weaving (e.g., ASM or cglib).

勿忘心安 2024-09-08 04:51:48

也许,您应该查看 java.reflect.Proxy。但是它要求您为要监视其方法调用的每个类创建一个代理。

一个例子:

    @SuppressWarnings("unchecked")
  public static <T> List<T> traceList(final List<T> list) {
    return (List<T>)Proxy.newProxyInstance(
      ProxyTest.class.getClassLoader(),
      new Class<?>[]{List.class}, 
      new InvocationHandler() {
        public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
          System.out.println("enter "+method);
          try {
           return method.invoke(list,args);
          } finally {
            System.out.println("exit "+method);
          }
        }
      });
  }

Maybe, you should look at java.reflect.Proxy.But it requires that you create one proxy per class for which you want to monitor method calls.

An example :

    @SuppressWarnings("unchecked")
  public static <T> List<T> traceList(final List<T> list) {
    return (List<T>)Proxy.newProxyInstance(
      ProxyTest.class.getClassLoader(),
      new Class<?>[]{List.class}, 
      new InvocationHandler() {
        public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
          System.out.println("enter "+method);
          try {
           return method.invoke(list,args);
          } finally {
            System.out.println("exit "+method);
          }
        }
      });
  }
明月松间行 2024-09-08 04:51:48

如果您只需要为自己的类执行此操作,而不需要为 Java 库中的类或从其他人那里获得的类执行此操作,那么这就是面向对象编程的全部内容。创建顶级类,将此函数放入其中,然后从那里派生任何其他类。

If you only need to do this for your own classes and not for classes from the Java library or that you get from someone else, then this is what Object-Oriented Programming is all about. Create your top level class, put this function in it, and then derive any other classes from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文