传说中的谷歌招聘委员会成员之一,从幕后走出来,教你学系统设计!


一亩三分地论坛

 找回密码
 获取更多干活,快来注册
天天打游戏、照样领工资、还办H1B
这份工作你要不要?
把贵司招聘信息放这里
查看: 1840|回复: 5
收起左侧

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

[复制链接] |试试Instant~ |关注本帖
fjj0916 发表于 2016-3-17 18:59:37 | 显示全部楼层 |阅读模式

[其他]CS 61B Data Structures(in Java) #1 - 2014spring@Berkeley

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干活,快来注册

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

查看全部评分

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? 这样可行吗?
回复 支持 反对

使用道具 举报

本版积分规则

关闭

一亩三分地推荐上一条 /5 下一条

手机版|小黑屋|一亩三分地论坛声明

custom counter

GMT+8, 2017-9-26 20:56

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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