一亩三分地论坛

 找回密码
 获取更多干货,去instant注册!

扫码关注一亩三分地公众号
查看: 1213|回复: 3
收起左侧

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

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

您需要 登录 才可以下载或查看,没有帐号?获取更多干货,去instant注册!

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"?

求大神指导下
回复 支持 反对

使用道具 举报

本版积分规则

请点这里访问我们的新网站:一亩三分地Instant.

Instant搜索更强大,不扣积分,内容组织的更好更整洁!目前仍在beta版本,努力完善中!反馈请点这里

关闭

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

手机版|小黑屋|一亩三分地论坛声明 ( 沪ICP备11015994号 )

custom counter

GMT+8, 2016-12-3 13:07

Powered by Discuz! X3

© 2001-2013 Comsenz Inc. Design By HUXTeam

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