Redian新闻
>
大家写java class的时候是完全封装的么?
avatar
大家写java class的时候是完全封装的么?# Java - 爪哇娇娃
T*e
1
比方说,如果class A中有一个object B,
在set/get方法中,是直接赋值呢,还是先拷贝?
直接赋值:
B getB(){ return b; }
void setB( B b ){ this.b = b ; }
拷贝:
B getB(){ return new B(b); }
void setB( B b ){ this.b = new B(b) ; }
当年用c++编程的时候,除了container class之外,
都是用拷贝,这样可以防止引用对象/数据在别处被误删。
java基本上不存在这个问题。
当然了,如果直接赋值,数据在别处被修改就没法避免。
本来想都用拷贝方法来完全封装的,后来觉得还是直接赋值
比较快(写代码、执行)。不过直接赋值可能会导致调试
比较复杂,而且容易被abuse。
大家都是怎么处理这个问题的?
呵呵,可能我需要读一本Thinking in java之类的书。
by the way, 哪儿有电子版么?或者别的推荐?
多谢了先。
avatar
d*s
2

Because you don't need to aware "delete" an object,
you'd better use what you said "直接赋值".
The system has an automatic gabarge colletion mechanism.
You can't delete an object. If an object is still referenced,
it won't be deleted by the system as well.
Sometimes you have to use Copy. But most time just use reference.

【在 T*****e 的大作中提到】
: 比方说,如果class A中有一个object B,
: 在set/get方法中,是直接赋值呢,还是先拷贝?
: 直接赋值:
: B getB(){ return b; }
: void setB( B b ){ this.b = b ; }
: 拷贝:
: B getB(){ return new B(b); }
: void setB( B b ){ this.b = new B(b) ; }
: 当年用c++编程的时候,除了container class之外,
: 都是用拷贝,这样可以防止引用对象/数据在别处被误删。

avatar
m*t
3

I would simply have the setter take over the reference, rather than
making a copy, unless it is *real* important to ensure that the object
is completely shielded, in which case there are lots of complications -
you'd have to make sure to make a deep copy of the object, and the object(s)
need to properly implement equals() and hashCode() to deal with the
"split identity" issue, and then there is also the issue on the way out -
do you make another deep copy in the getter before passing it out to

【在 T*****e 的大作中提到】
: 比方说,如果class A中有一个object B,
: 在set/get方法中,是直接赋值呢,还是先拷贝?
: 直接赋值:
: B getB(){ return b; }
: void setB( B b ){ this.b = b ; }
: 拷贝:
: B getB(){ return new B(b); }
: void setB( B b ){ this.b = new B(b) ; }
: 当年用c++编程的时候,除了container class之外,
: 都是用拷贝,这样可以防止引用对象/数据在别处被误删。

avatar
m*t
4

Oops, I forgot to rephrase how garbage collection works in my post,
I guess that puts me in the "have no idea what gc is" group, too. LOL
I am glad we agree on this, for once, though.

【在 d****s 的大作中提到】
:
: Because you don't need to aware "delete" an object,
: you'd better use what you said "直接赋值".
: The system has an automatic gabarge colletion mechanism.
: You can't delete an object. If an object is still referenced,
: it won't be deleted by the system as well.
: Sometimes you have to use Copy. But most time just use reference.

avatar
m*t
5

On a side note,
I'm not even sure it's a good idea to make it a general rule in C++,
either, because of complications similar to what I mentioned.
What I used to do in C++ is to define clearly in the API contract
who owns whom at which point. E.g., the setter would clearly document
that it would not make a copy of the object passed in, and would
take over the lifecycle management of the object, and the caller
shall forfeit all rights to the object thereafter and shall not
hold the setter liable

【在 T*****e 的大作中提到】
: 比方说,如果class A中有一个object B,
: 在set/get方法中,是直接赋值呢,还是先拷贝?
: 直接赋值:
: B getB(){ return b; }
: void setB( B b ){ this.b = b ; }
: 拷贝:
: B getB(){ return new B(b); }
: void setB( B b ){ this.b = new B(b) ; }
: 当年用c++编程的时候,除了container class之外,
: 都是用拷贝,这样可以防止引用对象/数据在别处被误删。

avatar
d*s
6

Don't spend time on arguing with me. Spend time on learning ANT JAR
task! Next time somebody asks "What doesn manifest mean in Export or create
a jar", don't answer "sorry, I don't know. I always use ANT to create jars"...

【在 m******t 的大作中提到】
:
: On a side note,
: I'm not even sure it's a good idea to make it a general rule in C++,
: either, because of complications similar to what I mentioned.
: What I used to do in C++ is to define clearly in the API contract
: who owns whom at which point. E.g., the setter would clearly document
: that it would not make a copy of the object passed in, and would
: take over the lifecycle management of the object, and the caller
: shall forfeit all rights to the object thereafter and shall not
: hold the setter liable

avatar
m*t
7

jars"...
Go back and read the OP, see if you rephrased exactly what was said by
both parties.
There is a difference between what *you think* other people are
saying and what other people are really saying, you know.
I only asked "what 'export'?", because honestly I have never used that
feature in Eclipse. In fact, I didn't even know it was there until
the OP relied.
But then, you feel free to jump to whatever conclusion you wish, my friend.

【在 d****s 的大作中提到】
:
: Don't spend time on arguing with me. Spend time on learning ANT JAR
: task! Next time somebody asks "What doesn manifest mean in Export or create
: a jar", don't answer "sorry, I don't know. I always use ANT to create jars"...

avatar
T*e
8
同志们,别太较真,大家都友好一点。
争论归争论,观点不同也没有什么哪。
有时候知道别人有不同想法也不错啊。
呵呵,我从你们的争论中学了不少东西。
好些时候都是一边看你们的帖子,一边google一些文档看看。

create

【在 m******t 的大作中提到】
:
: jars"...
: Go back and read the OP, see if you rephrased exactly what was said by
: both parties.
: There is a difference between what *you think* other people are
: saying and what other people are really saying, you know.
: I only asked "what 'export'?", because honestly I have never used that
: feature in Eclipse. In fact, I didn't even know it was there until
: the OP relied.
: But then, you feel free to jump to whatever conclusion you wish, my friend.

avatar
T*e
9
Yes, you guys are right. I am going for direct assignment.

【在 m******t 的大作中提到】
:
: jars"...
: Go back and read the OP, see if you rephrased exactly what was said by
: both parties.
: There is a difference between what *you think* other people are
: saying and what other people are really saying, you know.
: I only asked "what 'export'?", because honestly I have never used that
: feature in Eclipse. In fact, I didn't even know it was there until
: the OP relied.
: But then, you feel free to jump to whatever conclusion you wish, my friend.

avatar
T*e
10
当年用c++写的是一个比较复杂的系统,用完全封装的办法可以保证数据的一致性。
当时只要的问题是,如果一个对象被修改,那么与其相关联的对象或者属性也需要
做出调整。比方说,一条线段上的任何一个坐标发生变化,都可能导致整个图层
的拓扑重构。
当然了,如果api contract非常清晰而且每个程序员都非常清楚所有的tricks,
直接引用当然好,至少速度上能够快一些。当时没有这么做,主要是风险问题。

【在 m******t 的大作中提到】
:
: jars"...
: Go back and read the OP, see if you rephrased exactly what was said by
: both parties.
: There is a difference between what *you think* other people are
: saying and what other people are really saying, you know.
: I only asked "what 'export'?", because honestly I have never used that
: feature in Eclipse. In fact, I didn't even know it was there until
: the OP relied.
: But then, you feel free to jump to whatever conclusion you wish, my friend.

avatar
g*g
11
Deep copy doesn't make you any safer. How do you ensure
consistency of different copies?

【在 T*****e 的大作中提到】
: 当年用c++写的是一个比较复杂的系统,用完全封装的办法可以保证数据的一致性。
: 当时只要的问题是,如果一个对象被修改,那么与其相关联的对象或者属性也需要
: 做出调整。比方说,一条线段上的任何一个坐标发生变化,都可能导致整个图层
: 的拓扑重构。
: 当然了,如果api contract非常清晰而且每个程序员都非常清楚所有的tricks,
: 直接引用当然好,至少速度上能够快一些。当时没有这么做,主要是风险问题。

avatar
T*e
12
If every object takes good care of its own stuff, then
there is no consistency problem at all. For example:
class A {
B b;
public void setB(B b){
this.b = new B(b);
//do something to ensure self-consistency:
...
}
public B getB(){ return new B(b);}
}
When you call a.getB(), what you get is a copy of a.b.
You can do whatever you want to do to the copy, it won't
bring any trouble to a.
If you want to update the original copy, say a.b, you
call a.setB( b_copy ) and object a will

【在 g*****g 的大作中提到】
: Deep copy doesn't make you any safer. How do you ensure
: consistency of different copies?

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