需要澄清静态/动态类型和 Vtable
所以我拿回作业说我只答对了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确:静态类型始终是声明的类型,在本例中为 Base:
Base b = new Derived();
正确:动态类型是变量指向的类型,在本例中为派生类型
如果要考虑静态方法:
Base.m(String s)
;例如,这是一个奇怪的示例:
这会打印
Derived.m(String)
,为什么?a
的动态类型为 null,但Derived.m(String)
只关心a
的静态类型 DerivedCorrect: Static type is always the declared type, in this case Base:
Base b = new Derived();
Correct: Dynamic type is the type that the variable is pointing to, in this case Derived
If static methods were to be taken into account:
Base.m(String s)
would be called;Here's a bizarre example for instance:
This prints
Derived.m(String)
, why? The dynamic type ofa
is null, butDerived.m(String)
only cares about the static type ofa
which is Derived