查看: 4091| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

Berkeley CS 61B Data Structures(in Java) Lab5 讨论帖

全局:
公开课
学校名称: Berkeley
Unit号: 1
开课时间: 2014spring
课程全名: CS 61B Data Structures(in Java)
平台: 其他

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
我跟的是2014spring的~~
发现没有lab5,就把我自己的结果贴在这里了,有问题的话大家一起讨论~~~

Part I:  Assignments and Casting (1 point)
------------------------------------------
(a)  At compile-time, can we assign xa to ya, and vice versa?  When is a cast
     required?

Ans: ya = xa 不可以,但是反过来 xa = ya 是可以的。
ya = (Y[]) xa 这样也是可以的


(b)  At run-time, if ya references an array of Y's, can we assign it to xa?
     Can we then assign it back from xa to ya?

Ans: 可以

(c)  If xa references an array of X's (that are not Y's), can we assign it to
     ya?  Can we then assign it back from ya to xa?  Does it make a difference
     if the array of type X[] references objects that are all of class Y?  Why
     do you think this is the case?

Ans: 不可以 如果the array of type X[] references objects that are all of class Y的话,用cast就可以了

Part II:  Conflicting Declarations (1 point)
--------------------------------------------
(a)  Will Java compile the result?

Ans: 可以

(b)  What if the method declaration in the interface has a different return
     type?

Ans: 不可以,两个方法会冲突

(c)  What if the method declaration in the interface has the same return type,
     but a signature with a different parameter type?

Ans: 不可以,这样的话在subclass需要将interface里的方法实现了

(d)  What if the method declaration in the interface has the same return type,
     and the same number of parameters and parameter types, but those
     parameters have different names?

Ans: 可以

Part III:  More Conflicting Declarations (1 point)
--------------------------------------------------
(a)  Will Java compile the result?  Does it make any difference whether the
     constant in the superclass and the constant in the interface have the
     same value?

Ans: 可以,是不是同一个值无所谓

(b)  Write a main() method in the subclass that accesses the constant using the
     same name used in the superclass and the Java interface.  Will Java
     compile the result?  Does it make any difference whether the constant in
     the superclass and the constant in the interface have the same value?

Ans: 不可以

(c)  Figure out how to modify your main() method so that it accesses and prints
     one of the two conflicting values.  (Look to the Lecture 9 notes for
     clues.)  Make sure your code compiles and runs without errors.

Ans: 比如我的constant是FEB,我可以用X.FEB,Z.FEB分别得到父类的和接口的constant(不知道他说的是不是这个意思


Part IV:  Method Overriding (1 point)
-------------------------------------
(a)  Define a variable whose static type is the subclass and which references
     an object of the subclass.  If we cast the variable to the superclass type
     before calling the overridden method

        ((Superclass) subclassvariable).method();

     does Java call the superclass method or the subclass method?

Ans: 调用的是the subclass method

(b)  Define a variable whose static type is the superclass and which references
     an object of the superclass (but not the subclass).  If we cast the
     variable to the subclass type before calling the method, does Java call
     the superclass method or the subclass method?

Ans: run-time error 不能cast to the subclass type

(c)  Suppose you have an object whose class is the subclass.  Can you figure
     out a way to call the superclass method on that object without having to
     go through the subclass method of the same name?


Ans: 没太理解这个题什么意思(欢迎大家讨论



评分

参与人数 1大米 +10 收起 理由
squallee + 10 回答的很好!

查看全部评分


上一篇:征人一起学CS 61B
下一篇:[Coursera]求组队一起讨论作业 Stanford Algorithms: Design and Analysis, Part 1
🔗
Eloise 2016-4-3 11:46:00 | 只看该作者
全局:
Part I:
a.ya = xa; // error
   xa = ya; // OK
   ya = (Y[])xa;// OK
b.xa = ya;// OK
back  fail
c.fail;   still fail.   JAVA compiles code 1 line at a time,不知道xa指向的是Y

Part II:
a.OK
b.error
c.error
d.OK

Part III:
a.OK 不影响
b.都可以输出
c.此处假设 public class Y extends X implements Z
在X,Z中均设置x0的值,X中go(int num)返回x0,
System.out.print(Z.x0);
System.out.println(x.go(1));

Part IV:
a.subclass method
b.fail ?? 无法实现,throw exception
c.在Y中写一个method:
int useSuper(){
        return super.go(3);
}
然后利用y调用useSuper()




补充内容 (2016-4-8 18:21):
更正:
part2 b.back  ya = (Y[])xa; //OK
c.run-time error ;if the array of type X[] references objects that are all of class Y, 可行
Part3:b.compile-time error
orz...
回复

使用道具 举报

🔗
Eloise 2016-4-3 11:47:07 | 只看该作者
全局:
你的Part I 的(b)是怎么写的.. 我好像一直都error >_<
回复

使用道具 举报

🔗
kawayipk 2016-4-11 13:51:11 | 只看该作者
全局:
最后一个问题我也没读懂,我的理解是:
(c)  Suppose you have an object whose class is the subclass.  Can you figure
     out a way to call the superclass method on that object without having to
     go through the subclass method of the same name?
     *********************************************
     I'm not sure whether I got the meaning, but I can. If a method in superclass is static
     and overriden in subclass, let me suppose that I have a subclass object, which method
     will be invoked depends on the reference variable. If a superclass variable references
     the subclass object and invoke the static method, the superclass static method will be
     invoked, whereas if a subclass variable references the subclass object, the subclass
     static method will be invoked.
     I'm not sure the whether it's correct to define "override a static method", and if so,
     could I explian that "there isn't polimorphism in static method"?

求大神指导下
回复

使用道具 举报

🔗
布瓜小白 2017-5-17 11:15:17 | 只看该作者
全局:
打卡。bullet points:
1. static cannot reference to not-static field
2. super cannot be used in static
回复

使用道具 举报

🔗
yagamy 2017-7-30 06:11:11 | 只看该作者
全局:
打卡。
最后一道题(Part IV (c))我觉得上课笔记已经讲的蛮详细的,我的理解如下:已知一个object (subObj)属于subclass,我们再create另外一个superObj属于superclass,这样我们可以assign superObj = subObj;
因为superObj的static type是super,可以直接调用superclass method,即superObj.method(),然后再把调用过method()的superObj反过去assign给subObj, 需要cast,即: subObj = (subclass)superObj.

不知道我的理解对不对,如果有错,请多多指教呀~~

补充内容 (2017-7-30 23:01):
我给忘记了dynamic lookup。。 superObj的dynamic type还是subclass,所以 superObj.method() 调用的还是subclass method。。。难道要用((superclass)subObj).method? 这样可行吗?
回复

使用道具 举报

🔗
smoothiethu 2019-9-10 07:18:17 | 只看该作者
全局:
这次lab好难呀。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表