需要澄清静态/动态类型和 Vtable

发布于 2024-12-21 04:43:57 字数 1370 浏览 1 评论 0原文

所以我拿回作业说我只答对了 15/30 分,但没有解释我做错了哪道题或为什么错了。我即将参加考试,我想在参加考试之前弄清楚这一点。因此,我希望有人能帮助我理解我的对/错是什么?为什么?

所以这是我的作业:

考虑以下 Java 程序:

   public class Base {

    public        void m(Object o) { System.out.println("Base.m(Object)"); }
    public static void m(String s) { System.out.println("Base.m(String)"); }
    public        void m(Class c)  { System.out.println("Base.m(Class)");  }
   }

   public class Derived extends Base {

    public        void m(Object o) { System.out.println("Derived.m(Object)"); }
    public static void m(String s) { System.out.println("Derived.m(String)"); }
    public static void main(String[] args) {

      Base b = new Derived();
      b.m(new Object());
      b.m(new Integer(5));
      b.m("Hello");
      b.m(b.getClass());
    } 
   }
  • 问题 1(5 分):Derived.main() 中 b 的静态类型是什么?

    我的答案:基础

  • 问题 2(5 分):Derived.main() 中 b 的动态类型是什么?

    我的答案:Derived

  • 问题 3(20 分):Derived 的 vtable 中正确排序的条目是什么? 翻译?请使用classname.methodname(typenames)”表示法。 另外,请忽略第一个条目,即 __isa。

    我的答案:Derived 的 vtable-

    • 派生.m(对象)
    • Base.m(类)

    我的印象是 Base.m(String s) 和 Derived.m(String s) 都是静态方法;因此它们不会被包含在 vtable 中。另外,我假设将使用 Derived.m(Object) 而不是 Base.m(Object),因此不会包含在 Derived 的 Vtable 中。

So I got my homework back saying I only got 15/30 points correct, but there was no explanation on which question(s) I got wrong or why it was wrong. I have an exam coming up and I'd like to figure this out before I have to take it. Thusly I was hoping someone could help me understand what I got right/wrong? and why?

So here was my assignment:

Consider the following Java program:

   public class Base {

    public        void m(Object o) { System.out.println("Base.m(Object)"); }
    public static void m(String s) { System.out.println("Base.m(String)"); }
    public        void m(Class c)  { System.out.println("Base.m(Class)");  }
   }

   public class Derived extends Base {

    public        void m(Object o) { System.out.println("Derived.m(Object)"); }
    public static void m(String s) { System.out.println("Derived.m(String)"); }
    public static void main(String[] args) {

      Base b = new Derived();
      b.m(new Object());
      b.m(new Integer(5));
      b.m("Hello");
      b.m(b.getClass());
    } 
   }
  • Question 1 (5 Points): What is the Static Type of b in Derived.main()?

    My Answer: Base

  • Question 2 (5 Points): What is the Dynamic type of b in Derived.main()?

    My Answer: Derived

  • Question 3 (20 Points): What are the correctly ordered entries in Derived’s vtable for our
    translator? Please use classname.methodname(typenames)” notation.
    Also, ignore the first entry, which is __isa.

    My Answer: Derived’s vtable-

    • Derived.m(Object)
    • Base.m(Class)

    I'm under the impression that Base.m(String s) and Derived.m(String s) are both static methods; thus they would not be included in the vtable. Also, I assumed that Derived.m(Object) would be used instead of Base.m(Object) and thus would not be included in Derived's Vtable.

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

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

发布评论

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

评论(1

⒈起吃苦の倖褔 2024-12-28 04:43:57

问题1(5分):Derived.main()中b的静态类型是什么?

正确:静态类型始终是声明的类型,在本例中为 Base:Base b = new Derived();

问题2(5分):Derived.main()中b的动态类型是什么?

正确:动态类型是变量指向的类型,在本例中为派生类型

问题 3(20 分):对于我们的翻译器来说,Derived 的 vtable 中正确排序的条目是什么?请使用classname.methodname(typenames)”表示法。另外,请忽略第一个条目,即 __isa。

  • Derived.m(Object) 覆盖 Base.m(Object):Derived.m(Object) 正确
  • Base.m(c类)重载 Derived.m(对象):Base.m(c类)是正确的

如果要考虑静态方法:

  • Derived.m(String s) 将影子 Base.m(String s)。当方法被隐藏时,该方法纯粹依赖于静态类型;在这种情况下,静态类型是 Base,因此将调用 Base.m(String s)

例如,这是一个奇怪的示例:

Derived a = null;
a.m("World");

这会打印 Derived.m(String),为什么? a 的动态类型为 null,但 Derived.m(String) 只关心 a 的静态类型 Derived

Question 1 (5 Points): What is the Static Type of b in Derived.main()?

Correct: Static type is always the declared type, in this case Base: Base b = new Derived();

Question 2 (5 Points): What is the Dynamic type of b in Derived.main()?

Correct: Dynamic type is the type that the variable is pointing to, in this case Derived

Question 3 (20 Points): What are the correctly ordered entries in Derived’s vtable for our translator? Please use classname.methodname(typenames)” notation. Also, ignore the first entry, which is __isa.

  • Derived.m(Object) overrides Base.m(Object): Derived.m(Object) is correct
  • Base.m(Class c) overloads Derived.m(Object): Base.m(Class c) is correct

If static methods were to be taken into account:

  • Derived.m(String s) would shadow Base.m(String s). When methods are shadowed, the method depends purely on the static type; in this case the static type is Base and therefore Base.m(String s) would be called;

Here's a bizarre example for instance:

Derived a = null;
a.m("World");

This prints Derived.m(String), why? The dynamic type of a is null, but Derived.m(String) only cares about the static type of a which is Derived

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