avatar
java的一个问题# Java - 爪哇娇娃
A*e
1
这一阵累死了,所以发了这届比赛的获奖名单和奖金发放情况贴以后,就没在来这,没
想到没两天游戏版就发了这么大的水。我去看了hopeman说的精华区有关这届比赛的介
绍。
其实没啥啊,hopeman你想太多了。所谓精华区有关这届比赛的介绍,就是把这届比赛
的有关的贴子整理一下,弄进精华区目录,因为帖子多和精华区操作也很费时间的。我
查了下时间,老德从11/17号晚开始整理,到现在,不可能弄得完,还有很多呢。我去
翻了下旧帖子,太多了,没翻全,我能想起的就是拉架重写过他们组的介绍,帖子会考
前些吧。再说老德是七组海阔天空粉丝,要偏哪会偏剑队。
谢谢老德整理精华区。不过多劳没功劳,留给下届吧。。。kidding,我有时间也会去整
理的。。
avatar
b*d
2
请问这段代码为什么结果是"furry bray stripes", 同一个object, 用不同reference
type引用, object里面的variable的值就会变吗,还是本身object里面就有好几个同名
vairable分属于inheritence tree里面不同classes?
3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
7. class Zebra extends Mammal {
8. String name = "stripes ";
9. String makeNoise() { return "bray "; }
10.}
11. public class ZooKeeper {
12. public static void main(String[] args) { new ZooKeeper().go(); }
13. void go() {
14. Mammal m = new Zebra();
15. System.out.println(m.name + m.makeNoise() + ((Zebra)m).name);
16. }
17. }
avatar
h*n
3
这些大水得问老得,他自己惹的众怒。
那个问题是我队友问的,版务出来澄清比较好,以免误会。
随便问一下,最后一场比赛怎么算?给你写信了也没见回信,我的2个队员问了我好几
次了,请给个明确答复。早点了结,我也不用再辛苦陪练。

【在 A***e 的大作中提到】
: 这一阵累死了,所以发了这届比赛的获奖名单和奖金发放情况贴以后,就没在来这,没
: 想到没两天游戏版就发了这么大的水。我去看了hopeman说的精华区有关这届比赛的介
: 绍。
: 其实没啥啊,hopeman你想太多了。所谓精华区有关这届比赛的介绍,就是把这届比赛
: 的有关的贴子整理一下,弄进精华区目录,因为帖子多和精华区操作也很费时间的。我
: 查了下时间,老德从11/17号晚开始整理,到现在,不可能弄得完,还有很多呢。我去
: 翻了下旧帖子,太多了,没翻全,我能想起的就是拉架重写过他们组的介绍,帖子会考
: 前些吧。再说老德是七组海阔天空粉丝,要偏哪会偏剑队。
: 谢谢老德整理精华区。不过多劳没功劳,留给下届吧。。。kidding,我有时间也会去整
: 理的。。

avatar
r*l
4
说实话,这是个很基本的Java的问题。认真地读一本Java地书,就会很清楚这个问题。
Member variable的值和reference type有关。
所以我一直建议用private member,然后getter/setter。

reference

【在 b******d 的大作中提到】
: 请问这段代码为什么结果是"furry bray stripes", 同一个object, 用不同reference
: type引用, object里面的variable的值就会变吗,还是本身object里面就有好几个同名
: vairable分属于inheritence tree里面不同classes?
: 3. class Mammal {
: 4. String name = "furry ";
: 5. String makeNoise() { return "generic noise"; }
: 6. }
: 7. class Zebra extends Mammal {
: 8. String name = "stripes ";
: 9. String makeNoise() { return "bray "; }

avatar
A*e
5
有裁判长在,他会回信,所以刚才我看了信没回,没有时间你就别陪练了。不会影响比
赛结果。胜负已定。

【在 h*****n 的大作中提到】
: 这些大水得问老得,他自己惹的众怒。
: 那个问题是我队友问的,版务出来澄清比较好,以免误会。
: 随便问一下,最后一场比赛怎么算?给你写信了也没见回信,我的2个队员问了我好几
: 次了,请给个明确答复。早点了结,我也不用再辛苦陪练。

avatar
b*d
6

我知道,是应该encapsulate,自己编程肯定应该这样。
具体到这个问题,我也不是没看过java的书,不过还是没有解决我的问题
书上一般只说,编译器看reference type, 除了overridden method要看object type
但是我的问题是一个内部逻辑问题,为什么同一个object,假设只有一个instance
variable, 那为什么reference变了,值就变了;对我来说,唯一的解释就是这个
object其实保存着多个instance variable,每个variable对应他的一个superclass.
另外就是这个值是什么时候变的,一般instance variable 是在super()之后,this()
之前付值的,那我这里就改变一个reference就重新引起整个object 重新初始化吗?
如果不是这个object保存着这个variable的多个copy, 这是无法解释的。

【在 r*****l 的大作中提到】
: 说实话,这是个很基本的Java的问题。认真地读一本Java地书,就会很清楚这个问题。
: Member variable的值和reference type有关。
: 所以我一直建议用private member,然后getter/setter。
:
: reference

avatar
h*n
7
那些押了马甲他们赢的赌徒也经常来问,这个给个明确结果比较好。

【在 A***e 的大作中提到】
: 有裁判长在,他会回信,所以刚才我看了信没回,没有时间你就别陪练了。不会影响比
: 赛结果。胜负已定。

avatar
r*l
8
There are two variables: parent's "name" and child's "name".
Child's "name" hides parent's "name" if you use child type reference. If you
use parent type reference, parent's "name" was un-hidden.

【在 b******d 的大作中提到】
:
: 我知道,是应该encapsulate,自己编程肯定应该这样。
: 具体到这个问题,我也不是没看过java的书,不过还是没有解决我的问题
: 书上一般只说,编译器看reference type, 除了overridden method要看object type
: 但是我的问题是一个内部逻辑问题,为什么同一个object,假设只有一个instance
: variable, 那为什么reference变了,值就变了;对我来说,唯一的解释就是这个
: object其实保存着多个instance variable,每个variable对应他的一个superclass.
: 另外就是这个值是什么时候变的,一般instance variable 是在super()之后,this()
: 之前付值的,那我这里就改变一个reference就重新引起整个object 重新初始化吗?
: 如果不是这个object保存着这个variable的多个copy, 这是无法解释的。

avatar
A*e
9
那就等裁判长回信。我翻了一下比赛规则,没有看到有关的胜负已定的情况,还得干啥
。博彩就是流局了吧,伪币退回。

【在 h*****n 的大作中提到】
: 那些押了马甲他们赢的赌徒也经常来问,这个给个明确结果比较好。
avatar
b*d
10
谢谢,跟我想的一样,可惜好像我看的几本书都没讲,包括kelly的scjp exam
那是不是可以推广一下,一个object里面保留一份所有superclass的variables?

you

【在 r*****l 的大作中提到】
: There are two variables: parent's "name" and child's "name".
: Child's "name" hides parent's "name" if you use child type reference. If you
: use parent type reference, parent's "name" was un-hidden.

avatar
s*t
11
队长,跟着我念:
那些押了双方比赛的下注者也经常来问,这个给个明确结果比较好。
多念几遍....

【在 h*****n 的大作中提到】
: 那些押了马甲他们赢的赌徒也经常来问,这个给个明确结果比较好。
avatar
g*g
12
No, child can only access public and protected variables from
parents object directly.
If you have hierachy in your data object, I wouldn't even use
public or default modifier. For an embedded class that's not
visible outside, public modifier for fields is fine.
Otherwise, just avoid the problem. It's more important to
avoid tricky issue like this than understanding why tricky issue is
tricky.

【在 b******d 的大作中提到】
: 谢谢,跟我想的一样,可惜好像我看的几本书都没讲,包括kelly的scjp exam
: 那是不是可以推广一下,一个object里面保留一份所有superclass的variables?
:
: you

avatar
s*t
13
“我翻了一下比赛规则”
呵呵
好像没看到任何的比赛,在胜负以定的情况下,最后一场比赛不了了之的。
你的意思是我们这第三届WmGame版和军版联合举办Mitbbs拖拉机大赛从开始就表太认真?
Again,我知道发了此帖,会被人说我们是得了便宜卖乖,我们想没完没了,我们太认
真太在乎,我们不会防手....
可是看了贴子我还是想任性一下:
这个决定是第三届WmGame版和军版联合举办Mitbbs拖拉机大赛的组委会的决定吗?

【在 A***e 的大作中提到】
: 那就等裁判长回信。我翻了一下比赛规则,没有看到有关的胜负已定的情况,还得干啥
: 。博彩就是流局了吧,伪币退回。

avatar
J*n
14
mark this, very practical
It's more important to avoid tricky issue like this than understanding why
tricky issue is tricky.

【在 g*****g 的大作中提到】
: No, child can only access public and protected variables from
: parents object directly.
: If you have hierachy in your data object, I wouldn't even use
: public or default modifier. For an embedded class that's not
: visible outside, public modifier for fields is fine.
: Otherwise, just avoid the problem. It's more important to
: avoid tricky issue like this than understanding why tricky issue is
: tricky.

avatar
O*e
15
最近太忙错过了博彩,反正比赛还没打,现在还能压马甲吗。。。

【在 s*********t 的大作中提到】
: 队长,跟着我念:
: 那些押了双方比赛的下注者也经常来问,这个给个明确结果比较好。
: 多念几遍....

avatar
b*d
16
谢谢,我编程肯定不会搞什么public variable,但是我准备考scjp, 想搞明白点。
child不能access parent's private/default variable这个我知道,不过貌似系统开辟
child object内存的时候,给他所有parent的varialbe都开了空间,否则不能解释以上
现象: 比如改reference type就可以access parent's variable
另外一个类似的情况是child可以通过继承的public method access parent's private
variable也说明系统给parent's variable开辟了内存空间,并且初始化了

【在 g*****g 的大作中提到】
: No, child can only access public and protected variables from
: parents object directly.
: If you have hierachy in your data object, I wouldn't even use
: public or default modifier. For an embedded class that's not
: visible outside, public modifier for fields is fine.
: Otherwise, just avoid the problem. It's more important to
: avoid tricky issue like this than understanding why tricky issue is
: tricky.

avatar
A*e
17
我没这样说吧,我只是让等裁判长回信。你这哪是任性。。。

真?

【在 s*********t 的大作中提到】
: “我翻了一下比赛规则”
: 呵呵
: 好像没看到任何的比赛,在胜负以定的情况下,最后一场比赛不了了之的。
: 你的意思是我们这第三届WmGame版和军版联合举办Mitbbs拖拉机大赛从开始就表太认真?
: Again,我知道发了此帖,会被人说我们是得了便宜卖乖,我们想没完没了,我们太认
: 真太在乎,我们不会防手....
: 可是看了贴子我还是想任性一下:
: 这个决定是第三届WmGame版和军版联合举办Mitbbs拖拉机大赛的组委会的决定吗?

avatar
g*g
18
这个没有什么奇怪的,子类的constructor本来就必须调用父类的
constructor,如果没有写出来地话,就是父类无参数的default
constructor。
这也是为啥父类如果没有default constructor,子类就必须有
constructor,而且必须调用父类的constructor。

开辟
private

【在 b******d 的大作中提到】
: 谢谢,我编程肯定不会搞什么public variable,但是我准备考scjp, 想搞明白点。
: child不能access parent's private/default variable这个我知道,不过貌似系统开辟
: child object内存的时候,给他所有parent的varialbe都开了空间,否则不能解释以上
: 现象: 比如改reference type就可以access parent's variable
: 另外一个类似的情况是child可以通过继承的public method access parent's private
: variable也说明系统给parent's variable开辟了内存空间,并且初始化了

avatar
n*s
19
现实生活里,很多就不比了。比如乒乓球团体赛。
还可以看看世界杯三,四名的比赛多受人讨厌。

真?

【在 s*********t 的大作中提到】
: “我翻了一下比赛规则”
: 呵呵
: 好像没看到任何的比赛,在胜负以定的情况下,最后一场比赛不了了之的。
: 你的意思是我们这第三届WmGame版和军版联合举办Mitbbs拖拉机大赛从开始就表太认真?
: Again,我知道发了此帖,会被人说我们是得了便宜卖乖,我们想没完没了,我们太认
: 真太在乎,我们不会防手....
: 可是看了贴子我还是想任性一下:
: 这个决定是第三届WmGame版和军版联合举办Mitbbs拖拉机大赛的组委会的决定吗?

avatar
r*l
20
"child can only access public and protected variables from parents object
directly"

"一个object里面保留一份所有superclass的variables?"
并不冲突。
你这个“No”用在这里什么意思?

【在 g*****g 的大作中提到】
: No, child can only access public and protected variables from
: parents object directly.
: If you have hierachy in your data object, I wouldn't even use
: public or default modifier. For an embedded class that's not
: visible outside, public modifier for fields is fine.
: Otherwise, just avoid the problem. It's more important to
: avoid tricky issue like this than understanding why tricky issue is
: tricky.

avatar
y*9
21
世界杯三四场是赢钱好机会,很多人喜欢。必定逢赌必赢。

【在 n******s 的大作中提到】
: 现实生活里,很多就不比了。比如乒乓球团体赛。
: 还可以看看世界杯三,四名的比赛多受人讨厌。
:
: 真?

avatar
p*t
22
3. class Mammal {
4. String name = "furry ";
5. String makeNoise() { return "generic noise"; }
6. }
里面的name不是public variable,而是package member.
subclass can access to all the public,package and protected members of the
superclass.
找本教科书吧。

开辟
private

【在 b******d 的大作中提到】
: 谢谢,我编程肯定不会搞什么public variable,但是我准备考scjp, 想搞明白点。
: child不能access parent's private/default variable这个我知道,不过貌似系统开辟
: child object内存的时候,给他所有parent的varialbe都开了空间,否则不能解释以上
: 现象: 比如改reference type就可以access parent's variable
: 另外一个类似的情况是child可以通过继承的public method access parent's private
: variable也说明系统给parent's variable开辟了内存空间,并且初始化了

avatar
h*n
23
裁判长offically说最后那场比赛取消了,那就取消了吧。

真?

【在 s*********t 的大作中提到】
: “我翻了一下比赛规则”
: 呵呵
: 好像没看到任何的比赛,在胜负以定的情况下,最后一场比赛不了了之的。
: 你的意思是我们这第三届WmGame版和军版联合举办Mitbbs拖拉机大赛从开始就表太认真?
: Again,我知道发了此帖,会被人说我们是得了便宜卖乖,我们想没完没了,我们太认
: 真太在乎,我们不会防手....
: 可是看了贴子我还是想任性一下:
: 这个决定是第三届WmGame版和军版联合举办Mitbbs拖拉机大赛的组委会的决定吗?

avatar
b*d
24
default constructor那套规则我知道,不过子类调用父类constructor到底initialize
的是父类的variable,还是子类的variable书上都是没说的,是否为父类variable开辟内
存空间也是没有说的,我就是想搞清楚这个问题。
这样设计有个小问题,就是如果hirarchy tree太大,那底层的object就会因为保留各级
parent class的variables占用比较大的memory space

【在 g*****g 的大作中提到】
: 这个没有什么奇怪的,子类的constructor本来就必须调用父类的
: constructor,如果没有写出来地话,就是父类无参数的default
: constructor。
: 这也是为啥父类如果没有default constructor,子类就必须有
: constructor,而且必须调用父类的constructor。
:
: 开辟
: private

avatar
n*s
25
看看上届荷兰队的反应 - 其实之前就有人建议取消掉3,4名的比赛。那场比赛完全就
是让巴西找回点面子。

【在 y***9 的大作中提到】
: 世界杯三四场是赢钱好机会,很多人喜欢。必定逢赌必赢。
avatar
cr
26
唉,学过C++的就不会问这个问题了

initialize
辟内
各级

【在 b******d 的大作中提到】
: default constructor那套规则我知道,不过子类调用父类constructor到底initialize
: 的是父类的variable,还是子类的variable书上都是没说的,是否为父类variable开辟内
: 存空间也是没有说的,我就是想搞清楚这个问题。
: 这样设计有个小问题,就是如果hirarchy tree太大,那底层的object就会因为保留各级
: parent class的variables占用比较大的memory space

avatar
r*8
27
Even more important is for Java of Oracle to change its fundamental design
to make it more human-natural to eliminate this kind of tricky and brain
cell
killing problem and to let programmer more concentrate on business logic.
I don't understand why the hanlde = new zebra () does not point to starting
address of the child. The kind of fundamental design trap is well known for
long but Java's designer has never given a shit to it.

【在 J*******n 的大作中提到】
: mark this, very practical
: It's more important to avoid tricky issue like this than understanding why
: tricky issue is tricky.

avatar
g*y
28
What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent,
no matter what package the subclass is in. If the subclass is in the same
package as its parent, it also inherits the package-private members of the
parent. You can use the inherited members as is, replace them, hide them, or
supplement them with new members:
The inherited fields can be used directly, just like any other fields.
-------------------------------------------------------------------------
You can declare a field in the subclass with the same name as the one in
the superclass, thus hiding it (not recommended).
---------------------------------------------------------
You can declare new fields in the subclass that are not in the
superclass.
The inherited methods can be used directly as they are.
You can write a new instance method in the subclass that has the same
signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same
signature as the one in the superclass, thus hiding it.
You can declare new methods in the subclass that are not in the
superclass.
You can write a subclass constructor that invokes the constructor of the
superclass, either implicitly or by using the keyword super.

reference

【在 b******d 的大作中提到】
: 请问这段代码为什么结果是"furry bray stripes", 同一个object, 用不同reference
: type引用, object里面的variable的值就会变吗,还是本身object里面就有好几个同名
: vairable分属于inheritence tree里面不同classes?
: 3. class Mammal {
: 4. String name = "furry ";
: 5. String makeNoise() { return "generic noise"; }
: 6. }
: 7. class Zebra extends Mammal {
: 8. String name = "stripes ";
: 9. String makeNoise() { return "bray "; }

avatar
b*d
29
我的理解是
其实parent's variable都被"inherited"了, 只不过可能不能在subclass直接访问,要
通过适当的inherited (public) method, 或者cast to superclass (polymorphism)才
能访问。

or
in

【在 g****y 的大作中提到】
: What You Can Do in a Subclass
: A subclass inherits all of the public and protected members of its parent,
: no matter what package the subclass is in. If the subclass is in the same
: package as its parent, it also inherits the package-private members of the
: parent. You can use the inherited members as is, replace them, hide them, or
: supplement them with new members:
: The inherited fields can be used directly, just like any other fields.
: -------------------------------------------------------------------------
: You can declare a field in the subclass with the same name as the one in
: the superclass, thus hiding it (not recommended).

avatar
s*e
30
其实parent's variable都被"inherited"了, 只不过可能不能在subclass直接访问
You can think in that way. After all, super class's private variables will
be initialized after the constructor of a super class is called at run time.
But semantic visibility control is really the job of compiler. If you got
that wrong, the compiler wouldn't let you go. Also you might want to know
that semantic visibility is controlled at class level not at object level.
avatar
b*d
31

time.
Do you mean its controlled by reference type instead of object type?

【在 s******e 的大作中提到】
: 其实parent's variable都被"inherited"了, 只不过可能不能在subclass直接访问
: You can think in that way. After all, super class's private variables will
: be initialized after the constructor of a super class is called at run time.
: But semantic visibility control is really the job of compiler. If you got
: that wrong, the compiler wouldn't let you go. Also you might want to know
: that semantic visibility is controlled at class level not at object level.

avatar
W*e
32
instance methods are accessed by runtime object type. the object type is
still Zebra.
instance variables are accessed by reference type. all parents variables
have a copy in the runtime memory.

【在 b******d 的大作中提到】
:
: time.
: Do you mean its controlled by reference type instead of object type?

avatar
s*e
33
No. I meant that the following method is legal:
class A{
private int i= -1;
public void test()
{
A a = new A();
a.i = 5;
}
}

【在 b******d 的大作中提到】
:
: time.
: Do you mean its controlled by reference type instead of object type?

avatar
m*k
34
I think so,
you can simply try this,
class Mammal {
String name = "furry ";
protected String protected_name = "protected ";
private String private_name = "private ";
String makeNoise() {
return "generic noise";
}
}
class Zebra extends Mammal {
String name = "stripes ";
String makeNoise() {
return "bray ";
}
}
public class ZooKeeper {
public static void main(String[] args) {
new ZooKeeper().go();
}
void go() {
Zebra z = new Zebra();
Mammal m = z; //new Zebra();
System.out.println(m.name + m.makeNoise() + ((Zebra) m).name);
System.out.println(z.name + z.protected_name );
}
}
in eclipse debug, mouse over z and m, from z you can see all fields from
parent class. but you can not access z.private_name.

【在 b******d 的大作中提到】
: 我的理解是
: 其实parent's variable都被"inherited"了, 只不过可能不能在subclass直接访问,要
: 通过适当的inherited (public) method, 或者cast to superclass (polymorphism)才
: 能访问。
:
: or
: in

avatar
m*k
35
not think so.
only A's member methods can see i.

【在 s******e 的大作中提到】
: No. I meant that the following method is legal:
: class A{
: private int i= -1;
: public void test()
: {
: A a = new A();
: a.i = 5;
: }
: }

avatar
b*i
36
but test is a member method, right?

【在 m*****k 的大作中提到】
: not think so.
: only A's member methods can see i.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。