匿名类、继承和重写

发布于 2024-12-04 11:45:23 字数 873 浏览 2 评论 0原文

public class A {
    public A() {
        foo();
    }

    private void foo() {
        System.out.print("A::foo ");
        goo();
    }

    public void goo() {
        System.out.print("A::goo ");
    }
}

public class B extends A {
public B() {
    foo();
}

public void foo() {
    System.out.print("B::foo ");
}

public void goo() {
    System.out.print("B::goo ");
}

    public static void main(String[] args) {

A b = new B() {
        public void foo() {System.out.print("Anonymous::foo ");}
        public void goo() {((B)this).foo();}
        };

}
}

我希望您帮助理解为什么程序打印 A::foo Anonymous::foo Anonymous::foo。这个匿名类是不是取代了之前的B?重写它的方法?

在我看来,它应该转到 A 的默认构造函数,运行 A 的 foo- print "A::foo",而不是运行 B 的 goo,因为它已被正确覆盖,但现在 B 的 goo 是匿名类中的,所以它将其强制转换为 B (不执行任何操作),并运行 B 的 foo(即上面的 foo),因此它应该打印“Anonymous:foo”。我做错了什么?

多谢。

public class A {
    public A() {
        foo();
    }

    private void foo() {
        System.out.print("A::foo ");
        goo();
    }

    public void goo() {
        System.out.print("A::goo ");
    }
}

public class B extends A {
public B() {
    foo();
}

public void foo() {
    System.out.print("B::foo ");
}

public void goo() {
    System.out.print("B::goo ");
}

    public static void main(String[] args) {

A b = new B() {
        public void foo() {System.out.print("Anonymous::foo ");}
        public void goo() {((B)this).foo();}
        };

}
}

I'd like your help with understanding why does the program print A::foo Anonymous::foo Anonymous::foo. Is this anonymous class replace the former B? overrides its methods?

As I see it, it should go to A's default constructor, run A's foo- print "A::foo", than run B's goo, since it was properly overrided, but now B's goo is the one in the Anonymous class, so it casts this to B (Which does nothing), and run its foo, which is the foo above, of B, so it should print "Anonymous:foo". What do I get wrong?

Thanks a lot.

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

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

发布评论

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

评论(4

水水月牙 2024-12-11 11:45:23

你的问题不太清楚,但我只想说,如果你有一个顶级类 C,而不是扩展 B 的匿名类,那么答案将完全相同 扩展 B。匿名类在多态性和继承方面的行为没有什么不同。当 B 的构造函数调用 foo() 时,将调用最派生类(此处为匿名类)中的重写版本。

Your question isn't all that clear, but let me just say that the the answer would be exactly the same if instead of an anonymous class extending B, you had a top-level class C extending B. Nothing about anonymous classes makes them behave differently with respect to polymorphism and inheritance. When B's constructor calls foo(), the overriding version in the most-derived class -- here the anonymous class -- is invoked.

貪欢 2024-12-11 11:45:23

我认为这里令人困惑的是你有两个 foo 方法。一个是私有的,因此不符合覆盖条件,另一个是公共的,因此可以被覆盖。 B 在其构造函数中调用 foo 但被其子类覆盖。

I think the confusing thing here is you have two foo methods. One is private so it's not eligible for overriding, the other is public so it can be overridden. B is calling foo in its constructor but that's overridden by its subclass.

旧时光的容颜 2024-12-11 11:45:23

A 的构造函数调用 A.foo (A::foo),因为它是私有的,因此不会重载。 A.foo 调用 goo() ,它被 B 覆盖,然后被 Anonymous 覆盖,所以你得到 Anonymous.goo -> Anonymous.foo(匿名::foo)。然后 B 的构造函数调用 foo ,它被 Anonymous 覆盖,所以 (Anonymous::foo)

A's constuctor calls A.foo (A::foo) because it is private and so not overloaded. A.foo calls goo() which was overridden by B and then by Anonymous so you get Anonymous.goo -> Anonymous.foo (Anonymous::foo). Then B's constructor calls foo which is overridden by Anonymous so (Anonymous::foo)

柠檬心 2024-12-11 11:45:23

使用这种匿名构造实际上创建了 B 的子类。您已使用在匿名类中提供的方法覆盖了 B 的方法,因此将使用这些方法。

Using that kind of anonymous construction in fact creates a subclass of B. You have overridden B's methods with the ones you provide in the anonymous class so those will be used instead.

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