由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - java的一个问题
相关主题
SCJP书上某习题一问一道java题
问题征解Re: 初级问题
Java的method不都是virtual的么?private就不同了?Re: How to use abstract class?
who can help me with this dummy Question??Re: question
看了zhaoce073大水忍不住说2句Re: Why my url can not open a connection to my servlet in a web browse
请教个garbage collector问题Re: 如何在两个窗口之间通信?
问个基本问题Java interview Question(31-50)
有熟悉Java Reflection的吗Core Java2 Notes (4)
相关话题的讨论汇总
话题: string话题: subclass话题: zebra话题: superclass话题: name
进入Java版参与讨论
1 (共1页)
b******d
发帖数: 794
1
请问这段代码为什么结果是"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. }
r*****l
发帖数: 2859
2
说实话,这是个很基本的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 "; }

b******d
发帖数: 794
3

我知道,是应该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

r*****l
发帖数: 2859
4
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, 这是无法解释的。

b******d
发帖数: 794
5
谢谢,跟我想的一样,可惜好像我看的几本书都没讲,包括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.

g*****g
发帖数: 34805
6
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

J*******n
发帖数: 2901
7
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.

b******d
发帖数: 794
8
谢谢,我编程肯定不会搞什么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.

g*****g
发帖数: 34805
9
这个没有什么奇怪的,子类的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开辟了内存空间,并且初始化了

r*****l
发帖数: 2859
10
"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.

相关主题
请教个garbage collector问题一道java题
问个基本问题Re: 初级问题
有熟悉Java Reflection的吗Re: How to use abstract class?
进入Java版参与讨论
p*********t
发帖数: 2690
11
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开辟了内存空间,并且初始化了

b******d
发帖数: 794
12
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

cr
发帖数: 143
13
唉,学过C++的就不会问这个问题了

initialize
辟内
各级

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

r********8
发帖数: 186
14
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.

g****y
发帖数: 212
15
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 "; }

b******d
发帖数: 794
16
我的理解是
其实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).

s******e
发帖数: 493
17
其实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.
b******d
发帖数: 794
18

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.

W*******e
发帖数: 1268
19
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?

s******e
发帖数: 493
20
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?

相关主题
Re: questionJava interview Question(31-50)
Re: Why my url can not open a connection to my servlet in a web browseCore Java2 Notes (4)
Re: 如何在两个窗口之间通信?菜鸟问题
进入Java版参与讨论
m*****k
发帖数: 731
21
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

m*****k
发帖数: 731
22
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;
: }
: }

b***i
发帖数: 3043
23
but test is a member method, right?

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

S******1
发帖数: 269
24
When you override a method, it will act based on the dynamic reference.
When you override a property, it will act based on the static reference.
You probably need to see this video:
http://www.youtube.com/watch?v=OD2o6dNZIwA&feature=BFa&list=EC4
1 (共1页)
进入Java版参与讨论
相关主题
Core Java2 Notes (4)看了zhaoce073大水忍不住说2句
菜鸟问题请教个garbage collector问题
请问protected的使用问个基本问题
what's inside an java object?有熟悉Java Reflection的吗
SCJP书上某习题一问一道java题
问题征解Re: 初级问题
Java的method不都是virtual的么?private就不同了?Re: How to use abstract class?
who can help me with this dummy Question??Re: question
相关话题的讨论汇总
话题: string话题: subclass话题: zebra话题: superclass话题: name