覆盖Java中没有子类化的方法 [英] overriding methods without subclassing in Java

查看:89
本文介绍了覆盖Java中没有子类化的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始了一个新项目,第一次看到了如下所示的覆盖使用。

I started on a new project recently and saw the usage of overriding like below for the first time.

public class SomeClass {
  public void myMethod() {
    XStream xstream = new XStream() {
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
 // the rest ommitted

基本上,它覆盖了wrapMapper()方法在Thoughtworks xstream api中的XStream类,但没有SomeClass来扩展XStream类。我和Java一起工作了很多年,但这是我第一次看到这样的压倒一切。有人可以解释它的内外情况吗?谢谢。

Basically, it's overriding the wrapMapper() method of the XStream class in the thoughtworks xstream api but without having SomeClass to extend the XStream class. I've worked with Java for a number of years but this is the first time I saw overriding being done like this. Can someone explain the ins and out of it? Thanks.

推荐答案

这是匿名内部类

这篇关于覆盖Java中没有子类化的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆