当您动态创建实例时,在 Java 中它被称为什么?

发布于 2024-12-27 10:50:58 字数 460 浏览 5 评论 0原文

在代码中,

class MyObject {
   public String doThing() {
      return "doh";
   }
}

class MyClass {
   private myObject = null;
   public MyClass() {
       myObject = new MyObject() {
           public String doThing() {
              return "huh?";
           }
       };
   }

当 myObject 被分配一个新对象时,它被称为什么?从技术上讲,我试图找出“doThing”是否覆盖了 MyObject 中的方法,或者它是否重新定义了它,但我不知道要搜索什么来找到答案 - 并且不知道在不知道它是什么的情况下要问什么问题当您动态创建对象的新实例并为其提供实现时调用。

In code,

class MyObject {
   public String doThing() {
      return "doh";
   }
}

class MyClass {
   private myObject = null;
   public MyClass() {
       myObject = new MyObject() {
           public String doThing() {
              return "huh?";
           }
       };
   }

What is it called when myObject is assigned a new object? I'm technically trying to find out if the 'doThing' overrides the method from MyObject, or if it redefines it, but I have no idea what to search on to find an answer - and no idea what question to ask without knowing what it's called when you create a new instance of an object on the fly and give it an implementation.

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

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

发布评论

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

评论(5

随风而去 2025-01-03 10:50:58

您正在创建一个匿名内部类,它是MyObject的子类,所以是的,您正在重写doThing方法,如果这是您想要的问。

顺便说一句,匿名类就像命名类一样,它们在 .class 文件中有自己的字节码,其命名就像它们的封闭类一样,带有美元符号和数字后缀。

如果你想自己实验,可以使用myObject的方法getClass()并提取有关它的信息,如名称、父级、实现的接口、泛型参数, ETC。

You are creating an anonymous inner class that is a subclass of MyObject, so yes, you are overriding the doThing method, if is that what you ask.

By the way, anonymous classes are like named classes, they have their own bytecode in their .class file, that is named like their enclosing class suffixed with a dollar sign and an number.

If you want to experiment by yourself, you can use the method getClass() of myObject and extract information about it, like the name, parent, implemented interfaces, generic arguments, etc.

空‖城人不在 2025-01-03 10:50:58

这就是所谓的匿名内部类。

鉴于 doThing() 方法与其超类中的公共方法具有相同的签名,因此它会覆盖它。

最好的确定方法是在子类中的方法上添加@Override注解:如果带有此注解的方法没有重写任何方法,编译器将生成编译错误。

That's called an anonymous inner class.

Given that the doThing() method has the same signature as a public method in its superclass, it overrides it.

The best way to be sure is to add the @Override annotation to the method in the subclass: the compiler will generate a compilation error if the method with this annotation doesn't override any method.

过去的过去 2025-01-03 10:50:58

这个结构的名称是匿名内部类

你会发现很多关于这些的文档谷歌

The name for this structure is Anonymous inner class

You'll find plenty of docs about these with Google

深居我梦 2025-01-03 10:50:58

在Java 中,所有非最终实例方法都可以重写(即虚拟方法)。这同样适用于内部类,因此您的代码会覆盖 MyObjectdoThing() 方法。

In Java all non-final instance methods are subject to override (i.e. virtual). This equally applies to inner classes, so your code overrides MyObject's doThing() method.

少女的英雄梦 2025-01-03 10:50:58

是的,doThing() 方法被重写。这相当于一个匿名类继承 MyObject 的行为,然后重写它。

Yes, the doThing() method is overridden. This is equivalent to an anonymous class that is inheriting the behavior of MyObject and then overriding it.

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